Understanding Python Instance __dict__
Learn how Python's instance __dict__ stores attributes, how to inspect and modify it, and when __slots__ offers a better memory tradeoff.

Instances of most user-defined classes carry a special attribute called __dict__ that holds the instance's namespace. This dictionary maps attribute names to their values, and it is the mechanism that makes dynamic attribute assignment possible. When you write obj.attr = value, Python stores that binding in obj.__dict__ unless a descriptor on the class overrides the assignment (for example, a property or a slot member). Understanding how this dictionary works is useful for debugging, introspection, and choosing between the default attribute model and __slots__.
What Is the Instance dict?
The instance __dict__ is a plain dictionary that belongs to a specific object. For a class that does not define __slots__, each instance normally gets its own __dict__. The dictionary may be created lazily, but from Python code it behaves like any other dict: keys are strings naming attributes, and values are the current attribute values.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
p = Point(3, 4)
print(p.__dict__) # {'x': 3, 'y': 4}This dictionary is not a copy; it is the actual storage. Assigning a new attribute adds a key, and deleting an attribute removes it.
How Python Uses dict for Attribute Lookup
When you access obj.attr, Python first checks the type's MRO for a data descriptor (like a property or a slot member). If none exists, it looks in the instance __dict__. This is why setting obj.attr = value works even if the class doesn't declare attr in __init__. After the instance dict is checked, Python looks up non-data descriptors and class attributes in the MRO.
Because the instance dictionary is a regular dict, ordinary attribute access is a dictionary lookup on the instance after Python has checked for data descriptors. This is fast for most applications, but it does involve hashing the attribute name string. For code that accesses attributes in hot loops, this overhead can become measurable.
Inspecting and Modifying dict Directly
You can read and modify __dict__ directly, which is useful for metaprogramming, serialization, and debugging. For example, you can iterate over all instance attributes without knowing their names ahead of time.
for name, value in obj.__dict__.items():
print(f'{name} = {value}')You can also update it directly:
obj.__dict__['new_attr'] = 42This bypasses any __setattr__ logic, so use it with care. Direct mutation is common in libraries that implement dynamic behavior, such as ORMs or configuration systems, but it can break invariants if the class relies on property setters or validation.
slots and the Tradeoff with dict
Declaring __slots__ in a class tells Python to reserve a fixed set of attribute slots and not create a __dict__ for each instance. This saves memory because a dictionary has significant overhead: each entry stores the key, value, and a hash, plus the dict object itself. For millions of instances, the difference is substantial.
class SlottedPoint:
__slots__ = ('x', 'y')
def __init__(self, x, y):
self.x = x
self.y = yWith __slots__, instances no longer have a __dict__. Attempting to set an attribute not listed in __slots__ raises an AttributeError. This tradeoff gives you memory efficiency but reduces flexibility: you cannot add arbitrary new attributes to an instance.
The choice depends on your use case. If you have a fixed data structure and instantiate many objects, __slots__ is often the right call. If you need dynamic attributes or are building a prototype where flexibility matters more than memory, the default __dict__ is simpler.
Performance and Memory Considerations
The instance __dict__ is a regular Python dict, so its memory footprint includes hash-table overhead in addition to the stored attribute values. In contrast, __slots__ stores attribute values in a more compact per-instance C-level array.
Attribute access through a dict can also be slightly slower than slot access because it involves a dictionary lookup by name in addition to the usual class descriptor checks. Slot access uses a descriptor that indexes into a fixed array. The difference is usually small; profile your code before changing a design for performance.
There is also a runtime cost during object construction: filling a __dict__ for every instance adds allocation work, which can increase garbage collection pressure if you create many short-lived objects. Using __slots__ avoids that per-instance dict allocation.
Common Pitfalls and Edge Cases
Not every object has a __dict__. Instances of built-in types like int, str, and list do not have one. Classes themselves have a __dict__ that stores class attributes, but that is separate from the instance dictionary. Also, if a class defines __slots__, instances may still have a __dict__ if a base class without slots is present, or if '__dict__' is explicitly included in __slots__.
The instance __dict__ is provided by a descriptor on the type, so it is not simply another key stored inside the instance namespace. Directly mutating it can bypass normal attribute-setting behavior, such as __setattr__ or validation. In practice, direct mutation is mostly useful in metaprogramming or generic object tooling.
When using copy.copy or pickle, the default implementations use the instance __dict__ for ordinary objects. With __slots__, slot values are also part of the object state, which adds complexity to custom serialization.
When to Rely on dict vs. slots
The instance __dict__ is the backbone of Python's dynamic attribute model. It is the right choice when you need flexibility: adding attributes on the fly, introspecting object state, or building generic utilities that operate on any object. It is also the default, so it requires no extra code.
Use __slots__ when you have a well-defined schema, you are creating a large number of instances, and memory usage is a concern. The tradeoff is that you lose the ability to add new attributes dynamically, and you must be careful with inheritance and serialization.
For most application code, the default __dict__ is perfectly adequate. The performance difference is rarely the bottleneck. If you suspect it is, profile first and then consider __slots__ only if measurements justify the added rigidity.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.