Self .__ dict__ = self value in class definition

I am trying to understand the following piece of code:

class Config(dict): def __init__(self): self.__dict__ = self 

What is the purpose of the line self.__dict__ = self ? I suppose it overrides the default __dict__ with something that just returns the object itself, but since Config inherits from dict , I could not find any difference with the default behavior.

+5
source share
2 answers

The purpose of the dictionary self - __dict__ allows access to the attribute and access to the element:

 >>> c = Config() >>> c.abc = 4 >>> c['abc'] 4 
+5
source

According to the Python Document , object.__dict__ :

A dictionary or other mapping object used to store attributes of objects (written).

The following is an example of an example:

 >>> class TestClass(object): ... def __init__(self): ... self.a = 5 ... self.b = 'xyz' ... >>> test = TestClass() >>> test.__dict__ {'a': 5, 'b': 'xyz'} 
+3
source

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


All Articles