Instance and Class Attributes
__init__ is a special method that runs automatically when an object is instantiated, marking the beginning of the object's lifecycle.
Try it: Classes, Instances and Attributes
How a class creates instances with __init__ and self, why an attribute read checks the instance's __dict__ before the class, why assigning through an instance never changes the class, how a class-level list is shared by every instance, and why obj.method(x) is Class.method(obj, x).
How it works
- Dog(...) makes a new, empty instance and calls Dog.__init__(new_object, ...); self.name = ... stores into that object's __dict__.
- Reading obj.attr looks in obj.__dict__ first, then in the class.
- Assigning obj.attr = value always writes obj.__dict__, so it shadows a class attribute for that one object only.
- Mutating a class-level list (self.tricks.append) changes the one list every instance shares.
- obj.method is found on the class and bound to obj, so obj.method(x) runs Class.method(obj, x).
Default run (30 steps): Ready: class Dog and the statements you picked. Watch where each attribute is found and where each assignment writes. … Finished. Printed: ['sit'] / Wolf / Canis familiaris / Fido is 5.
Simplified: The lab's own small Python interpreter runs a fixed, hand-written script (you choose the variant and the values, never the code); nothing is executed as real Python. Every script was run in CPython 3.12 with the same inputs and the lab reproduces its output and error messages. Object numbers are illustrative, not real id() values.
Loading the simulation…
From Class to Object
A class provides the basis for creating objects, called instances. The important event for this topic happens when a new instance is created: Python automatically runs the special __init__ method. That method begins the object's lifecycle by giving the new object its initial attributes.
What do you think happens?
A class has an __init__ method. When a new instance is created, what should you expect to happen next?
Reveal answer
The method is called automatically
__init__ is automatically called whenever a new instance of the class is created. Its job is to initialize the object's attributes and begin its initial state.
The Creation Sequence
The sequence can be traced in three stages. First, Python has a class definition. Next, a program creates a new instance from that class. Finally, Python automatically calls __init__ for that new instance. The call to __init__ marks the beginning of the object's lifecycle and gives the method an opportunity to establish sensible starting values.
- 1A class is available as the basis for creating instances.
- 2The program creates a new instance of that class.
- 3Python automatically calls the class's
__init__method. - 4
__init__initializes the attributes belonging to that particular instance. - 5The instance begins its lifecycle with an initial, predictable state.
Connecting self to One Object
The self parameter in __init__ refers to the specific object being created. This is what allows initialization to affect one instance rather than all instances produced from the class. When __init__ attaches an attribute through self, that attribute belongs to the particular object represented by self.
Two Independent Initializations
Imagine a class used to create profile objects. Two profile instances are created, one for a learner named Amina and one for a learner named Ravi.
- 1Create the first instance
Python creates Amina's profile instance and automatically calls
__init__. During this call, self refers to Amina's particular profile object. - 2Initialize the first instance
The attributes assigned through self become part of Amina's own initial state.
- 3Create the second instance
Python creates Ravi's profile instance and automatically calls
__init__again. This time, self refers to Ravi's particular profile object. - 4Initialize the second instance
The attributes assigned through self become part of Ravi's own initial state, separate from Amina's attributes.
The same __init__ method is used for both objects, but each call initializes a different object because self refers to the specific instance being created.
Building Initial State
An object's initial state is the collection of attribute values it has immediately after __init__ finishes. The method's purpose is not merely to exist; it initializes the object's attributes to sensible starting values. A well-initialized object therefore begins in a valid and predictable state.
Always initialize every attribute that the object will use in __init__. This gives each instance a complete starting setup and helps ensure that its state is valid and predictable from the beginning of its lifecycle.
Class and Instance Roles
The class and its instances have different roles. The class is the source from which objects are created. Each instance is a particular object created from that class, with its own separate set of attributes initialized by its own call to __init__. The class therefore supplies the common structure for creation, while initialization gives each resulting object its independent state.
| Class | Instance |
|---|---|
| Provides the basis for creating objects | Is one particular object created from the class |
| Has the __init__ method that is automatically called during instance creation | Receives its own initialization through a call to __init__ |
| Can produce multiple instances | Has its own separate set of attributes |
The source pack emphasizes the relationship between a class and its instances and the independent initialization of each instance.
Common Initialization Mistakes
Common mistake Treating __init__ as a method that must be called separately after every object is created
What it looks likeA learner creates an instance and assumes its attributes are uninitialized until they manually call __init__.
Why it fails__init__is automatically called whenever a new instance is created.Do this insteadTrace instantiation first, then place the automatic __init__ call immediately after the new instance is created.
Common mistake Forgetting that self identifies the particular instance
What it looks likeA learner thinks one call to __init__ updates every object created from the class.
Why it failsself refers to the specific object being created, so each instance is initialized independently.
Do this insteadAsk which object self refers to during each individual call to __init__.
Common mistake Leaving an attribute out of __init__ even though the object will use it
What it looks likeAn object is expected to use a status attribute, but that attribute is not initialized when the object begins its lifecycle.
Why it failsThe object may not start in a valid, predictable state.
Do this insteadInitialize all attributes the object will use in __init__.
Common mistake Confusing the class with one of its instances
What it looks likeA learner describes the class itself as though it were the individual object receiving a separate set of initialized attributes.
Why it failsThe class is the source for creating instances, while each instance has its own separate attributes.
Do this insteadUse the class as the creation source and identify the individual instance whose state is being initialized.
Trace the Mechanism
A class is used to create two objects: Object One and Object Two. Each object is created at a different time, and the class has an __init__ method that assigns the object's starting attributes. Describe the execution flow in order, including what self refers to during the first call and during the second call.
Hints
- Begin with the class as the source for creating instances.
- Place an automatic
__init__call after each object is created. - Remember that self refers to a different specific object in each call.
Tracing Two Object Creations
Explain what happens when a class creates Object One and then Object Two, assuming both objects use the class's __init__ method.
- 1Start with the class
The class is available as the basis for creating objects.
- 2Create Object One
A new instance is created, and Python automatically calls
__init__for it. - 3Bind self for Object One
During the first call, self refers to Object One, so the attributes initialized through self belong to Object One.
- 4Create Object Two
A second new instance is created, and Python automatically calls
__init__again. - 5Bind self for Object Two
During the second call, self refers to Object Two, so its attributes are initialized separately from Object One's attributes.
Both objects follow the same initialization process, but each receives its own initial state because each call to __init__ uses self for a different instance.
Key Takeaways
- 1
__init__is a special method that runs automatically when a new instance is created. - 2This automatic call marks the beginning of the object's lifecycle.
- 3self refers to the specific object being created, so its attributes can be initialized independently.
- 4Every instance has its own separate set of attributes initialized by its own call to
__init__. - 5Initialize every attribute the object will use in
__init__so each instance starts in a valid, predictable state.
At a glance
__init__runs automatically during the creation of every new instance.- The method establishes the object's initial state by initializing its attributes.
- self connects the method to the particular instance currently being created.
- Instances created from one class are initialized separately and have independent sets of attributes.
- A reliable
__init__initializes all attributes that the object will use.