Python __dict__

The attribute __dict__must contain user-defined attributes. But if we print an __dict__empty class, I would also get:

__module__
__dict__ 
__weakref__
__doc__

which are pre-populated by Python in the attribute __dict__according to the type of the class object.

Now __base__and __class__also are class attributes of the class class of the Python class, but are not included in __dict__.

Is there any rule indicating which dunder attribute is included in the object __dict__and which are not?

+4
source share
1 answer

The attribute __dict__must contain user-defined attributes.

, __dict__ . , , .

, . . __getattribute__, object; type(object).__getattribute__(attribute_name), , , ( __dict__).

__bases__ , type(); :

>>> class Foo:
...     pass
...
>>> Foo.__bases__
(<class 'object'>,)
>>> type.__dict__['__bases__']
<attribute '__bases__' of 'type' objects>
>>> type.__dict__['__bases__'].__get__(Foo, type)
(<class 'object'>,)

__dict__ , . , , (__module__ __doc__), , (__dict__ __weakref__). , , type, .

, __bases__ , __doc__ ? __bases__ , . Python , , (, ).

+9

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


All Articles