We know that this creates a class:
class X:
a = 1
And, in Python 3, due to new style classes, X
automatically inherits from object
, but in Python 2, it doesn't:
>>> X.__bases__ # Python 2
()
>>> X.__bases__ # Python 3
(<class 'object'>,)
And we also know that we can dynamically create such a class using type factory:
X = type("X",(object,), {"a":1})
name^ ^bases ^ "class" body
However, if we omit the object
in the bases tuple just as we did with class
syntax, we inherit from object
in python3 and, unexpectedly, in python2 as well:
X = type("X", ( ), {"a":1})
^ empty bases tuple
>>> X.__bases__ # Python 2
(<type 'object'>,)
>>> X.__bases__ # Python 3
(<class 'object'>,)
I expected X.__bases__
to be empty in python 2.
And it's not such a documented feature, I hardly find something about this on internet.
In fact, python's official documentation contradictory states that:
the bases tuple itemizes the base classes and becomes the
__bases__
attribute
But, as shown above, ()
still leads to (<type 'object'>,)
in Python 2, so it's not really true that it becomes the __bases__
attribute.
Can anyone explain this behavior?