__dict__
The dictionary that holds an object's own attributes, or a class's own attributes.
In more detail
An object's __dict__ holds only its instance variables, the names assigned through self or through the object. A class's __dict__ holds its class variables and methods. Reading obj.name looks in the object's __dict__ first and then in its class, which is why a class variable can be read through an object without appearing in the object's __dict__.
Example
class T:
price = 10
def __init__(self):
self.seat = 'A1'
print(T().__dict__) # {'seat': 'A1'}Related terms
- class attribute — An attribute defined in the class body, shared by every instance of the class.
- instance attribute — An attribute stored on one instance, usually set as self.name = value, so each instance has its own copy.
- introspection — A program examining objects and classes at run time, for example with hasattr, getattr, type and isinstance.
Taught in
- Class Variables, Instance Variables And __dict__ — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Introspection: hasattr, __name__, __module__ And __bases__ — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Object-Oriented Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep