liveBook · Manning

__dict__

As explained in Python How-To

__dict__ is the dictionary attribute that stores an object's or class's dynamically assigned attributes when such a dictionary is available.

__dict__ is the dictionary attribute that exposes an object's stored instance attributes when that object has an attribute dictionary. Attribute names appear as keys, and their values appear as values. After a constructor assigns attributes, inspect the instance directly with task.__dict__. section 8.1.2 section 9.4.2

Inspecting constructor-created attributes

class Task: def __init__(self, title, desc, urgency): self.title = title self.desc = desc self.urgency = urgency task = Task("Laundry", "Wash clothes", 3) print(task.__dict__) # {'title': 'Laundry', 'desc': 'Wash clothes', 'urgency': 3} section 8.1.2

__dict__ also participates in ordinary instance-attribute lookup. When the dictionary contains a requested name, Python returns its value; when it does not, __getattr__ can provide a fallback. Leaving a lazy attribute unset allows that fallback to run, and setattr can cache the fetched value for later access. section 9.4.2

Use the dictionary representation deliberately:

  • Inspect an instance's currently stored attributes while developing or debugging a class. section 8.1.2
  • Copying task.__dict__ creates a dictionary snapshot, not another Task; the copied dictionary lacks class behavior such as save_data. section 10.3.1
  • Prefer copy(task) when the goal is an independent object that retains the class's methods and behavior. section 10.3.1

How Python How-To covers it

How deep it goes
Defines dictionary storage and a simplified lookup path, but stops short of the complete attribute-resolution order.
Examples
Demonstrates Task attribute inspection, lazy profile data, dictionary copying, and copying a complete Task instance.
What it assumes
Assumes familiarity with custom classes, instances, constructors, attributes, and dot notation.
What you can do after
Diagnose instance state and distinguish a copied attribute dictionary from a copied object that retains class behavior.
How it connects
Connects constructor assignment with __init__, fallback lookup with __getattr__, caching with setattr, and state copying with the copy module.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.