Need to represent several tasks, each with its own title and urgency? Define a class, then create an object for each task. An object created from a class is an instance of that class; creating it is called instantiation. This applies to custom classes and built-in classes such as list and dict. section 4.3.4 section 5.1
Create and inspect an instance
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} Calling Task supplies the values needed to initialize the new instance. Inside __init__, self refers to that instance, so assignments such as self.title give it attributes. Choose initialization arguments that leave an instance ready to use. section 8.1.2 section 8.6
A class call has two steps behind the scenes: __new__ creates the instance, then __init__ initializes that same object. You normally do not need to implement __new__ yourself. The id function can show that the object seen by these methods and the variable receiving the result has the same identity. section 10.2.1 section 8.1.1
Figure 10.2 The instantiation process of a custom class. After we call the constructor of a custom class, behind the scenes, the __new__ and __init__ methods are invoked sequentially, with __new__ creating the new object and __init__ completing the initialization process. In the end, the construction results in the creation of an instance object.
Follow the instance from the class call through __new__ and __init__ to the completed object. section 10.2.1
Once an instance exists, distinguish the object from the ways you use it:
- A variable refers to an object; it is not the object itself. Reassigning the variable changes which object it refers to. Lists and dictionaries can hold additional references to the same instance. section 10.2.3
- Instances can be collected in a list. To find a task by an attribute such as urgency, examine the instances in a loop and stop when the value matches. section 4.3.4
- If edits must be cancelable, make a copy for the changes while retaining the original instance as a backup. section 10.3
- An instance is not normally callable. A class that implements __call__ can make its instances callable—for example, an instance used as a decorator. section 10.5.4



