In the section on Custom Classes of The Python Language Reference it is stated that:
Special attributes:
__name__
is the class name;__module__
is the module name in which the class was defined;__dict__
is the dictionary containing the class’s namespace;__bases__
is a tuple (possibly empty or a singleton) containing the base classes, in the order of their occurrence in the base class list;__doc__
is the class’s documentation string, orNone
if undefined.
So, __bases__
for a custom class can "possibly be empty"? How can that be achieved if everything implicitly inherits from object
in Python 3
?
The only class with an empty __bases__
is object
itself:
>>> object.__bases__
()
Am I missing something?
The line has been there since before Python 3 was released. Here's the 2007 commit. When the object model was revamped to get rid of old-style classes, it looks like that information in the docs slipped through review and is likely just misinformation now.
May I suggest you raise a documentation issue to clear up whether it is incorrect/outdated.
That paragraph isn't so much wrong as misleading, due to where it appears: "Custom classes" is the only place in the Data Model docs where __bases__
is addressed at all. Since working with custom classes is the only time you're likely to care about the content of __bases__
, I guess that makes some sense.
The surprise is that it gives a complete description of __bases__
, including details that only apply to non-custom classes like the builtin, object
. As you pointed out, object.__bases__
is empty:
>>> object.__bases__
()
I think that phrase was supposed to be a heads-up that unknown_class.__bases__[0]
could raise an IndexError
, because even in Python 3, __bases__
could be empty.
__bases__
can also be a singleton, apparently. Why anyone would care a bout that, I can't imagine. Maybe by "singleton", they meant "1-tuple"? Er, OK... I still don't care.
Update: As wim has pointed out, the paragraph in question was imported along with all of Python 2's
Docs/reference/datamodel.rst
on Wed 15 Aug 2007, and has remained basically unchanged since then. The section it's in, however, was just "Classes" back then. That section was renamed to "Custom classes" on Fri 31 Aug 2007, as part of the effort to remove the "old-style" / "new-style" distinction.The addition of the word "Custom" does not seem like an improvement, since the bulk of that section applies to most or maybe all classes (I don't know to what extent it applies to classes compiled from another language).
PS: In case anyone's tempted to sneakily replace the __bases__
attribute after class creation, Python 3's got a special error message just for you...
>>> class A: pass
...
>>> A.__bases__
(<class 'object'>,)
>>> A.__bases__ = ()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only assign non-empty tuple to A.__bases__, not ()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With