Creating Objects from Classes
A class is a template that defines what data and code each object will have; the class keyword does not create an object.
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…
A Template Is Not an Object
When you first see the class keyword, it is easy to think that writing a class has already created an object. It has not. A class is a template that defines the data and code objects of that type will have. An object is an individual instance created from that template.
A cookie cutter provides a useful analogy. The cutter is the template, while each cookie made with it is a separate object. In Python, the class is the template and each object created from it is an instance with its own data.
What Happens During Creation
Object creation begins when you call the class as if it were a function. For example, an = PartyAnimal() creates a new PartyAnimal object. As part of that creation, Python automatically runs the object's __init__ method. That method sets up the object's initial attributes.
After an = PartyAnimal(), the object has an.x set to 0.How self Selects the Object
The self parameter in a method refers to the specific object instance on which that method was called. It gives the method access to that object's attributes, so the method can read or modify the correct object's data.
class PartyAnimal:
def __init__(self):
self.x = 0
def party(self):
self.x = self.x + 1
print(self.x)
an = PartyAnimal()
an.party()
an.party()
an.party()- 1The class defines a party method that changes an object's x attribute.
- 2an =
PartyAnimal()creates an object and initializes its x attribute to 0. - 3The first
an.party()call changesan.xfrom 0 to 1 and prints 1. - 4The second call changes
an.xfrom 1 to 2 and prints 2. - 5The third call changes
an.xfrom 2 to 3 and prints 3.
self.x= 0 gives the new object an initial x attribute.self.x=self.x+ 1 reads the object's current x value and stores the increased value.- When
an.party()is called, self refers to an.
1
2
3A Complete Class Walkthrough
Tracing Three Method Calls
Create a PartyAnimal object, initialize its counter, and call its party method three times.
- 1Define the class
The class definition describes the data and behavior that PartyAnimal objects will have. Defining it does not create a PartyAnimal object.
- 2Create the object
The expression an =
PartyAnimal()calls the class. Python creates an object and automatically runs__init__for that object. - 3Initialize the attribute
The
__init__method assigns 0 toself.x. For this call, self refers to an, so the object's x value starts at 0. - 4Call party three times
Each
an.party()call runs with self referring to an. The method increases the same object's x value by 1 on each call.
After the three calls, an.x is 3.
1
2
3The important sequence is class definition, object creation, initialization, and method calls. The class supplies the structure and behavior. The object carries its own current attribute value. The method uses self to update that object's state.
Mistakes Beginners Make
Common mistake Assuming that the class keyword creates an object.
What it looks likeclass PartyAnimal:Why it failsThis defines the template and its behavior, but it does not create an individual PartyAnimal object.
Do this insteadCall the class, as in an = PartyAnimal(), to create an object.
Common mistake Treating __init__ as an ordinary method that must be called manually after object creation.
What it looks likean = PartyAnimal()Why it failsThe
__init__method runs automatically when the object is created and sets up its initial attributes.Do this insteadPut initial attribute assignments in __init__ and allow object creation to trigger it.
Common mistake Leaving self out of an instance method's access to object data.
What it looks likex = x + 1Why it failsThe method needs self to refer to the specific object's attribute.
Do this insteadUse self.x when reading or modifying the object's x attribute.
Common mistake Thinking that one method call changes every object created from the class.
What it looks likean.party()Why it failsself refers to the particular object on which the method was called.
Do this insteadTrack the object before the dot. In an.party(), self refers to an.
Practice the Creation Sequence
Write a class named Counter with an __init__ method that gives self.value an initial value of 0. Add a method named increase that adds 1 to self.value. Then create an object named counter and call increase twice. Before checking your work, state what value counter.value should have after the two calls.
Hints
- Define the class before creating the object.
- Initialize the attribute with
self.valueinside__init__. - Use
self.valueboth when reading the current value and when storing the increased value.
What do you think happens?
If an object starts with x equal to 0 and its method increases self.x by 1, what is x after three method calls?
Reveal answer
3
The first call changes the value to 1, the second to 2, and the third to 3. Each call uses self to modify the same object's attribute.
The Object-Oriented Pattern
- 1A class is a template that defines the data and code objects will have; defining the class does not create an object. Calling the class creates an object. The
__init__method runs automatically during creation and establishes the object's initial attributes. Methods use self to refer to the specific object that called them. Because each object has its own attributes, one class can create multiple objects with independent data.
Key Takeaways
- A class is a template, while an object is an individual instance created from that template.
- The class keyword defines structure and behavior; it does not create an object by itself.
- Calling a class creates an object and automatically runs its
__init__method. - The
__init__method establishes initial attributes such asself.x= 0. - The self parameter connects a method to the particular object's attributes that the method reads or changes.