Modifying Object State with Methods
A method is a function that belongs to a class and includes a self parameter that refers to the specific object the method is called on.
Behavior Belongs with Data
A method is a function that belongs to a class. It combines behavior with the data represented by an object. When a method is called on an instance, Python automatically passes that particular object into the method through the parameter named self.
The central question for every instance method is: which object is this method working with? The self parameter provides that answer.
A Method Definition
The def keyword begins the method definition. The method name comes next, followed by parentheses containing self. The indented statements form the method body. Even when the method needs no information from the caller, self remains in the definition because the method still needs to receive the object on which it was called.
Tracing the Calling Instance
Suppose p and q are two Person instances. In p.say_hi(), self refers to p. In q.say_hi(), self refers to q. The method definition is shared by the class, but the automatically supplied self value changes according to the instance before the dot.
class Person: def say_hi(self): print("Hi!") p = Person() p.say_hi() q = Person() q.say_hi()
Changing Object State
A method can do more than print text. Because self identifies the object being used, the method can work with that object's attributes and perform an action on its behalf. The following generated example illustrates a method that changes an attribute representing the object's state.
class Counter: def increase(self): self.value = self.value + 1 counter = Counter() counter.value = 0 counter.increase() print(counter.value)
Before the call, the generated example gives counter a value of 0. During counter.increase(), self refers to counter, so self.value identifies the value belonging to that object. After the method runs, the object's value is 1. This is the basic pattern behind modifying object state: a method receives the object through self and performs an operation using that object.
Calling Without Extra Arguments
| Method definition | Call | Additional caller data |
|---|---|---|
| def say_hi(self): | p.say_hi() | None |
| def greet(self, name): | p.greet("Mina") | name receives "Mina" |
A method can take no additional parameters beyond self. In that case, the caller writes the method name followed by empty parentheses, as in p.say_hi(). The empty parentheses do not mean that self is missing; Python supplies self automatically. They mean that the caller is not supplying any other argument.
A method can also be invoked directly on a class constructor, as in Person().say_hi(). Person() creates a temporary Person object, and say_hi is called on that object immediately. This approach is useful when the object does not need to be stored for later use.
Hi!
Hi!Mistakes with self
Leaving self out of the method definition when the method takes no other parameters.
Every method definition needs self so Python has a parameter through which it can pass the object that called the method.
Fix:
Write def say_hi(self):Assuming the caller must type self as an argument.
Python automatically passes p as self when the method is called with dot notation.
Fix:
Write p.say_hi() when the method takes no additional parameters.Treating self as one permanently fixed object.
self refers to the specific instance used for the current call.
Fix:
Trace the object before the dot: self is p for p.say_hi() and q for q.say_hi().Using a separate function instead of a method when the behavior belongs with the class data.
Methods bundle behavior with data and make class-based code more organized and maintainable.
Fix:
Define the behavior inside the class when it belongs to the objects created from that class.
When reading an instance method call, identify the object before the dot first. Then substitute that object mentally for self while tracing the method body. This makes it easier to determine which object's attributes or state the method can access or modify.
Trace the Method Call
Consider the generated Counter example. Trace counter.increase() in order. Identify the object selected by the expression, the value received by self, and the state change that occurs after the method body runs.
Hints
- Look at the object before the dot.
- The method definition contains only self, so the caller supplies no additional argument.
- The method changes the value associated with self.
Tracing p.say_hi()
A Person class defines say_hi(self), and the call p.say_hi() is made. What does self refer to, and what does the method do?
Select the instance: The object before the dot is p, so p is the instance on which the method is called.
Supply self: Python automatically passes p to the method through its self parameter.
Run the body: The method body executes with self referring to p. In the source-shaped say_hi example, the body prints Hi!.
Check for a return value: The example method performs the printing action and does not return a value.
During p.say_hi(), self refers to p, and the method performs its print action on behalf of that object.
Summary
- A method is a function that belongs to a class.
- Every method definition includes self, even when there are no additional parameters.
- Python automatically passes the instance used in a dot-notation call as self.
- The same method can operate on different objects because self changes with the calling instance.
- Methods bundle behavior with data and can perform actions or modify an object's state.
Key Takeaways
- A method is a class-owned function that receives its calling object through self.
- self is required in every method definition because it identifies the object the method works with.
- In p.say_hi(), Python supplies p as self automatically.
- A method with only self needs no additional argument from the caller.
- Methods can use the object identified by self to perform behavior and modify object state.