Custom classes with empty __bases__ attribute

The Custom Classes section of the Python Language Reference states that:

Special attributes: __name__ is the name of the class; __module__ is the name of the module in which the class was defined; __dict__ - a dictionary containing the class namespace; __bases__ is a tuple (possibly empty or single) containing the base classes, in the order they appear in the list of the base class; __doc__ is a documentation line for classes, or None if undefined.

So, can __bases__ for a custom class be "empty"? How can this be achieved if everything implicitly inherits from object in Python 3 ?

The only class with empty __bases__ is object itself:

 >>> object.__bases__ () 

Did I miss something?

+5
source share
2 answers

The line has been there ever since Python 3 was released. Here is the 2007 commit . When the object model was updated to get rid of the old-style classes, it looks like this information in the documents has passed validation and, most likely, just misinformation.

May I suggest that you raise the documentation to find out if it is incorrect / outdated.

+2
source

This paragraph is not as erroneous as misleading because of where it appears: “Custom classes” is the only place in the Data Model documents where __bases__ is addressed at all. Since working with custom classes is the only time you are likely to care about the contents of __bases__ , I think that makes sense.

Surprisingly, it gives a complete description of __bases__ , including details that apply only to non-standard classes such as inline, object . As you pointed out, object.__bases__ empty:

 >>> object.__bases__ () 

I think the phrase was supposed to be head-up, that unknown_class.__bases__[0] could raise an IndexError , because even in Python 3, __bases__ could be empty.

__bases__ can also be single, apparently. Why not take care of what I cannot imagine. Maybe "singleton", they meant "1 tuple"? Er, OK ... I don't care.

Update: As wim pointed out, this paragraph was imported along with the entire Python 2 Docs/reference/datamodel.rst from Monday August 15, 2007 and has since remained virtually unchanged. However, the section in which he was located was simply “classes”. This section was renamed “Custom Classes” on Friday August 31, 2007 as part of an effort to remove the “old style” / “new style”.

Adding the word "Custom" does not seem to be an improvement, since the bulk of this section applies to most or perhaps all classes (I don’t know to what extent this applies to classes compiled from another language).

PS: In case someone is tempted to secretly replace the __bases__ attribute after creating the class, Python 3 received 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 () 
+2
source

Source: https://habr.com/ru/post/1260016/


All Articles