Understanding Attributes and Methods
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 natural to think that an object has already been created. Python separates these two ideas. A class is a template that defines the data and code objects of that type will have. The class keyword defines the template; it does not create an object. An object is created later by calling the class as if it were a function.
Tracing Object Creation
What do you think happens?
After the class has been defined, what does the statement an = PartyAnimal() do?
Reveal answer
Answer: It creates a new PartyAnimal object and automatically runs its __init__ method.
Objects are created by calling the class as if it were a function. During that creation, __init__ runs and sets up the object's initial attributes.
- Python first has the PartyAnimal class definition available as a template.
- The call PartyAnimal() creates a new object.
- Python automatically runs that object's __init__ method.
- The __init__ method gives the object its initial attribute values.
- The name an refers to the newly created object.
Attributes and Initialization
An attribute is data associated with an object. The __init__ method runs automatically when an object is created and sets up the object's initial attributes. This gives every new object an initialized starting state.
In this example, the class definition describes a kind of object. The __init__ method establishes an attribute named x, and the initial value assigned to that attribute is 0. The expression self.x refers to the x attribute belonging to the particular object being initialized.
How self Chooses the Object
The self parameter in a method refers to the specific object instance on which that method was called. It allows the method to access and modify that object's attributes.
The same class can produce many objects. Each object has its own independent data. When a method is called through one object, self connects the method to that object, so an expression such as self.x reads or changes the correct object's x attribute rather than the attribute belonging to a different object.
Methods Change Object 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 method is code associated with a class that can operate on an object. In this example, party uses self.x both to read the current value and to store the updated value. Because self refers to an, every call changes an's own attribute.
A Complete Execution Trace
Following one PartyAnimal object
Trace the object named an from creation through three calls to party.
Define the class: The PartyAnimal class becomes a template describing the data and methods its objects will have. No PartyAnimal object has been created merely by defining the class.
Create the object: The call PartyAnimal() creates a new object. Python automatically runs __init__ for that object.
Initialize the attribute: The initializer sets the object's x attribute to 0, so an begins with x equal to 0.
Call party three times: Each call binds self to an, increases an.x by 1, and prints the new value.
The object starts with x equal to 0. After three party calls, x is 3, and the printed values are 1, 2, and 3.
The important chain is class definition, object creation, automatic initialization, method call, and attribute update. The class supplies the structure, __init__ establishes the starting data, and self ensures that later methods use the correct object's data.
Mistakes Beginners Make
Assuming that writing class creates an object
The class keyword defines a template. An object is created only when the class is called.
Fix:
Separate the class definition from the later call that creates an object, such as PartyAnimal().Treating __init__ as an ordinary method that must be called manually
__init__ runs automatically when the object is created and sets up its initial attributes.
Fix:
Recognize object creation as the event that automatically invokes __init__.Forgetting that self identifies the current object
Methods need self to access and modify the attributes of the specific instance using the method.
Fix:
Use self when referring to the current object's attributes, such as self.x.Expecting every object from a class to share one changing attribute value
Each object has its own independent copy of the attributes defined for it.
Fix:
Track the attribute through the relevant self reference and object instance.
Practice the Object Lifecycle
Create a simple class based on the PartyAnimal pattern. Give it an __init__ method that sets an attribute to 0, then add a method that uses self to increase that attribute by 1. Describe what happens when an object is created and the method is called twice.
Hints
- Start by identifying the class template.
- Put the initial attribute assignment in __init__.
- Use self when reading and changing the object's attribute.
- Trace the attribute from its initial value through both method calls.
To check your reasoning, the object should begin with the value assigned in __init__. Each method call should operate on that same object's attribute through self, so two increments produce a value two greater than the starting value.
Essential Takeaways
- A class is a template for the data and code that its objects will have; defining a class does not create an object.
- Calling a class as if it were a function creates an object instance.
- The __init__ method runs automatically during object creation and establishes the object's initial attributes.
- The self parameter refers to the particular object using a method.
- Methods use self to read and modify that object's independent attributes.
- One class can create many objects, each with its own data.
Key Takeaways
- A class defines a reusable template, while an object is an instance created from that template.
- Calling the class creates an object and automatically runs its __init__ method.
- The __init__ method establishes the object's initial attributes.
- The self parameter connects each method call to the specific object whose data the method reads or changes.
- Methods can update an object's attributes, and each object maintains its own independent data.