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.
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()
A Complete Class Walkthrough
Tracing Three Method Calls
Create a PartyAnimal object, initialize its counter, and call its party method three times.
Define the class: The class definition describes the data and behavior that PartyAnimal objects will have. Defining it does not create a PartyAnimal object.
Create the object: The expression an = PartyAnimal() calls the class. Python creates an object and automatically runs __init__ for that object.
Initialize the attribute: The __init__ method assigns 0 to self.x. For this call, self refers to an, so the object's x value starts at 0.
Call 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
Assuming that the class keyword creates an object.
This defines the template and its behavior, but it does not create an individual PartyAnimal object.
Fix:
Call the class, as in an = PartyAnimal(), to create an object.Treating __init__ as an ordinary method that must be called manually after object creation.
The __init__ method runs automatically when the object is created and sets up its initial attributes.
Fix:
Put initial attribute assignments in __init__ and allow object creation to trigger it.Leaving self out of an instance method's access to object data.
The method needs self to refer to the specific object's attribute.
Fix:
Use self.x when reading or modifying the object's x attribute.Thinking that one method call changes every object created from the class.
self refers to the particular object on which the method was called.
Fix:
Track 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.value inside __init__.
- Use self.value both 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
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
- A 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 as self.x = 0.
- The self parameter connects a method to the particular object's attributes that the method reads or changes.