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
NewStyleandNewStyle2. Both are of typetypewhileOldStyleof typeclassobj.If you subclass from object, the
__class__ofobject(meaningtype) 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 fromobject,Py_ClassTypeis assigned as the metaclass for you.In all cases,
metaclass.__new__is going to get called. ForPy_ClassType.__new__it follows the semantics defined (I've never examined them, really) and fortype.__new__it makes sure to packobjectin the bases of your class.Of course, a similar effect is achieved by:
where a call is immediately made to
type; it's just a bigger hassle :-)