Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103431 > unrolled thread

How to define what a class is ?

Started by"ast" <nomail@invalid.com>
First post2016-02-24 09:08 +0100
Last post2016-02-24 20:11 +1100
Articles 6 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  How to define what a class is ? "ast" <nomail@invalid.com> - 2016-02-24 09:08 +0100
    Re: How to define what a class is ? Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-24 01:46 -0700
      Re: How to define what a class is ? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-02-25 09:44 +1300
      Re: How to define what a class is ? "ast" <nomail@invalid.com> - 2016-02-25 10:54 +0100
        Re: How to define what a class is ? eryk sun <eryksun@gmail.com> - 2016-02-25 05:21 -0600
    Re: How to define what a class is ? Ben Finney <ben+python@benfinney.id.au> - 2016-02-24 20:11 +1100

#103431 — How to define what a class is ?

From"ast" <nomail@invalid.com>
Date2016-02-24 09:08 +0100
SubjectHow to define what a class is ?
Message-ID<56cd64fb$0$9220$426a74cc@news.free.fr>
Hi

Since a class is an object, I ask myself 
how to define rigorously what a class is.

classes are instances from type, but not all, since
a class may be an instance of a metaclass

A class is always callable

A class inherit from some others classes, so they
have a bases attribute

any thing else ?

Suppose I provide to you an object and that I
ask to you to tell me if it is a class or not. How
would you proceed ?

thx

[toc] | [next] | [standalone]


#103434

FromIan Kelly <ian.g.kelly@gmail.com>
Date2016-02-24 01:46 -0700
Message-ID<mailman.85.1456303651.20994.python-list@python.org>
In reply to#103431
On Wed, Feb 24, 2016 at 1:08 AM, ast <nomail@invalid.com> wrote:
> Hi
>
> Since a class is an object, I ask myself how to define rigorously what a
> class is.
>
> classes are instances from type, but not all, since
> a class may be an instance of a metaclass

All metaclasses are subclasses of type, so all classes are instances of type.

> A class is always callable

It doesn't strictly have to be. You could override the __call__ method
in the metaclass to raise a TypeError if you don't want the class to
be callable. I don't know of a use case for this, though.

> Suppose I provide to you an object and that I
> ask to you to tell me if it is a class or not. How
> would you proceed ?

import inspect
inspect.isclass(x)

[toc] | [prev] | [next] | [standalone]


#103457

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2016-02-25 09:44 +1300
Message-ID<dj6j1jFd104U1@mid.individual.net>
In reply to#103434
Ian Kelly wrote:

> All metaclasses are subclasses of type, so all classes are instances of type.

I think that's about the most general definition you
can find. Almost everything else that you might think
of as being part of the classness of a class can be
overridden.

Another definition might be that it's something that
is potentially the result of type(x) for some x.
I think that's equivalent, because at the C level
type(x) returns the value of x->ob_type, which
always points to an instance of type or a subclass
thereof.

-- 
Greg

[toc] | [prev] | [next] | [standalone]


#103490

From"ast" <nomail@invalid.com>
Date2016-02-25 10:54 +0100
Message-ID<56cecf47$0$3305$426a74cc@news.free.fr>
In reply to#103434
"Ian Kelly" <ian.g.kelly@gmail.com> a écrit dans le message de 
news:mailman.85.1456303651.20994.python-list@python.org...
> On Wed, Feb 24, 2016 at 1:08 AM, ast <nomail@invalid.com> wrote:


> All metaclasses are subclasses of type, so all classes are instances of type.

Ah ! I didn't know that if an object Obj is an instance of Myclass and
MyClass inherit from MyClass2, then Ojb is an instance of MyClass2 too

>>> from enum import Enum, EnumMeta
>>>
>>> isinstance(Enum, EnumMeta)
True
>>> isinstance(EnumMeta, type)
True
>>> isinstance(Enum, type)
True

that's correct


>> Suppose I provide to you an object and that I
>> ask to you to tell me if it is a class or not. How
>> would you proceed ?
>
> import inspect
> inspect.isclass(x)

So we can conclude that inspect.isclass(x) is equivalent
to isinstance(x, type)

lets have a look at the source code of isclass:

def isclass(object):
    """Return true if the object is a class.

    Class objects provide these attributes:
        __doc__         documentation string
        __module__      name of module in which this class was defined"""
    return isinstance(object, type)


correct



[toc] | [prev] | [next] | [standalone]


#103492

Fromeryk sun <eryksun@gmail.com>
Date2016-02-25 05:21 -0600
Message-ID<mailman.121.1456399345.20994.python-list@python.org>
In reply to#103490
On Thu, Feb 25, 2016 at 3:54 AM, ast <nomail@invalid.com> wrote:
> So we can conclude that inspect.isclass(x) is equivalent
> to isinstance(x, type)
>
> lets have a look at the source code of isclass:
>
> def isclass(object):
>    """Return true if the object is a class.
>
>    Class objects provide these attributes:
>        __doc__         documentation string
>        __module__      name of module in which this class was defined"""
>    return isinstance(object, type)

Except Python 2 old-style classes (i.e. 2.x classes that aren't a
subclass of `object`) are not instances of `type`. Prior to new-style
classes, only built-in types were instances of `type`. An old-style
class is an instance of "classobj", and its instances have the
"instance" type.

    >>> class A: pass
    ...
    >>> type(A)
    <type 'classobj'>
    >>> type(A())
    <type 'instance'>

Note that "classobj" and "instance" are instances of `type`.

The `isclass` check in Python 2 has to instead check
isinstance(object, (type, types.ClassType)).

    >>> types.ClassType
    <type 'classobj'>

[toc] | [prev] | [next] | [standalone]


#103435

FromBen Finney <ben+python@benfinney.id.au>
Date2016-02-24 20:11 +1100
Message-ID<mailman.86.1456305124.20994.python-list@python.org>
In reply to#103431
"ast" <nomail@invalid.com> writes:

> Since a class is an object, I ask myself how to define rigorously what
> a class is.

A class is a type. This bears stating, because for a large part of
Python's history, the two were distinct.

A lot of Python documentation that has its roots in that history still
is careful to maintain the distinction, which in current Python is
completely obsolete.

Every type (and therefore every class) is a template for instances. The
type defines what values are possible for those instances, and also
defines what behaviour those instances will have in common.

> classes are instances from type, but not all, since a class may be an
> instance of a metaclass

Yes. Every class (every type) is an instance of a metaclass, and there
is a particular metaclass named ‘type’.

> A class is always callable

By default, calling a class creates an instance of that class, and
returns that instance to the caller.

> A class inherit from some others classes, so they have a bases
> attribute

That's not part of the definition, really. You need to know that, but
it's not necessary to say what a class is.

> any thing else ?

See the reference documentation on this topic
<URL:https://docs.python.org/3/reference/datamodel.html#objects-values-and-types>.

> Suppose I provide to you an object and that I ask to you to tell me if
> it is a class or not. How would you proceed ?

If the object is an instance of the ‘type’ metaclass, the object is a
type (i.e. a class).

Metaclasses are callable, and return a type (a class) to the caller.

The ‘type’ metaclass, if called with an object as a parameter, will
return the type of that object.

If you have the name ‘foo’ bound to an object, you can call::

    type(foo)

and the metaclass ‘type’ will return the instance of that object's type.
For example::

    >>> type("spam")
    <class 'str'>
    >>> type(None)
    <class 'NoneType'>
    >>> type(3)
    <class 'int'>
    >>> type(int)
    <class 'type'>
    >>> type(type)
    <class 'type'>

If you want a boolean test::

    >>> isinstance(3, type)
    False
    >>> isinstance("spam", type)
    False
    >>> isinstance(int, type)
    True
    >>> isinstance(type, type)
    True

-- 
 \        “The fact of your own existence is the most astonishing fact |
  `\    you'll ever have to confront. Don't dare ever see your life as |
_o__)    boring, monotonous, or joyless.” —Richard Dawkins, 2010-03-10 |
Ben Finney

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web