Old-style classes, new-style classes, and metaclasses

In Python 2.x, all new-style classes inherit from objectimplicitly or explicitly. Then look at this:

>>> class M(type):
...     pass
...
>>> class A:
...     __metaclass__ = M
...
>>> class B:
...     pass
...
>>> a = A()
>>> b = B()
>>> type(A)
<class '__main__.M'>
>>> type(a)
<class '__main__.A'>

Does this mean what Ais the class of the new style? But Adoes not inherit from object, anyway?

>>> type(B)
<class 'classobj'>
>>> type(b)
<type 'instance'>

OK, Ba classic class, right?

>>> isinstance(A, object)
True
>>> isinstance(B, object)
True

why instances Aand Binstances object?

If it Bis an instance object, then type(B)it won’t classobj, right?

+5
source share
2 answers

, : http://docs.python.org/reference/datamodel.html#customizing-class-creation. , . :

class M(type):
    pass

:

class C:
    __metaclass__ = M

, M ( type ). , , types.ClassType.

+5

All Articles