Python: explain __dict__ attribute

I really got confused in __dict__ . I searched a lot, but still I'm not sure about the conclusion.

Can someone explain the use of this attribute from scratch when it is used in an object, class or function?

+35
python
source share
1 answer

Basically it contains all the attributes that describe the object in question. It can be used to modify or read attributes. Quote from this

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

Remember that all this is an object in python. When I say everything, I mean everything as functions, classes, objects, etc. (I read it right, classes. Classes are also objects). For example,

 def func(): pass func.temp = 1 print func.__dict__ class TempClass(object): a = 1 def tempFunction(self): pass print TempClass.__dict__ 

Exit

 {'temp': 1} {'a': 1, '__module__': '__main__', 'tempFunction': <function tempFunction at 0x7f77951a95f0>, '__dict__': <attribute '__dict__' of 'TempClass' objects>, '__weakref__': <attribute '__weakref__' of 'TempClass' objects>, '__doc__': None} 
+43
source share

All Articles