__dict__Easy Examples

Dictionary holding an object's writable attributes

Accessing __dict__

How to access and use __dict__.

python
# __dict__ holds an object's attributes
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

p = Person("Alice", 30)
print(p.__dict__)
print(Person.__dict__.keys())

__dict__ is a special attribute that dictionary holding an object's writable attributes.

__dict__ in practice

Using __dict__ in everyday code.

python
# More __dict__ examples
print("__dict__ is a special Python attribute")

# Every object has certain dunder attributes
obj = object()
attrs = [a for a in dir(obj) if a.startswith("__")]
print(f"object has {len(attrs)} dunder attributes")

Understanding __dict__ helps you introspect and debug Python objects.

Want to try these examples interactively?

Open Easy Playground