How to define what a class is ?

ast nomail at invalid.com
Thu Feb 25 04:54:08 EST 2016


"Ian Kelly" <ian.g.kelly at gmail.com> a écrit dans le message de 
news:mailman.85.1456303651.20994.python-list at python.org...
> On Wed, Feb 24, 2016 at 1:08 AM, ast <nomail at 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






More information about the Python-list mailing list