This is an old-style class:
class OldStyle:
pass
This is a new-style class:
class NewStyle(object):
pass
This is also a new-style class:
class NewStyle2:
__metaclass__ = type
Is there any difference whatsoever between NewStyle
and NewStyle2
?
I have the impression that the only effect of inheriting from object
is actually to define the type
metaclass, but I cannot find any confirmation of that, other than that I do not see any difference.
Pretty much yes, there's no difference between NewStyle
and NewStyle2
. Both are of type type
while OldStyle
of type classobj
.
If you subclass from object, the __class__
of object
(meaning type
) is going to be used; if you supply a __metaclass__
that is going to get picked up.
If nothing is supplied as __metaclass__
and you don't inherit from object
, Py_ClassType
is assigned as the metaclass for you.
In all cases, metaclass.__new__
is going to get called. For Py_ClassType.__new__
it follows the semantics defined (I've never examined them, really) and for type.__new__
it makes sure to pack object
in the bases of your class.
Of course, a similar effect is achieved by:
cls = type("NewStyle3", (), {})
where a call is immediately made to type
; it's just a bigger hassle :-)