Calling Methods on Objects
The self parameter is the first parameter in every class method and refers to the instance itself.
The Object Behind the Call
When you call a method through an object, Python connects that method call to the particular instance on the left side of the dot. The first parameter in the method receives that instance. By convention, this parameter is named self.
Tracing the Automatic Argument
Consider the generated call myobject.method(arg1, arg2). Python automatically supplies myobject as the method's first argument. You write the object before the dot, but you do not write self in the call. The object is passed into the first parameter, which is conventionally named self.
Following the First Argument
Trace the generated call for myobject.method(arg1, arg2).
Start with the instance call: The call begins with myobject before the dot, followed by method and the arguments arg1 and arg2.
Place the instance first: Python uses myobject as the first argument supplied to the method.
Keep the remaining arguments in order: arg1 and arg2 remain after the instance.
Read the class-method form: The equivalent form is MyClass.method(myobject, arg1, arg2).
The object before the dot becomes the first argument in the class-method form.
Why the First Position Matters
self represents the instance itself. It occupies the first parameter position in a class method, while the arguments you supply in the call follow it. This position lets Python associate the method call with the particular object that appeared before the dot.
The Name self
Python allows other parameter names technically, but self is the established convention for the first parameter of a class method. Using self communicates to readers that this parameter refers to the instance itself. The convention makes the method's parameter positions easier to recognize when reading class definitions.
| Parameter name | Technical status | Meaning communicated to readers |
|---|---|---|
| self | Conventional | This is the instance itself |
| another name | Technically allowed | Less clearly communicates the standard instance parameter |
Mistakes with Method Calls
Treating self as an argument that must be written in the ordinary object call.
Python automatically passes the instance to the method.
Fix:
Call the method through the object without explicitly supplying self.Forgetting that the instance occupies the first parameter position.
The object before the dot is passed first, and the remaining arguments follow it.
Fix:
Trace the equivalent form MyClass.method(myobject, arg1, arg2).Replacing self with a different parameter name without a strong reason.
Other names are technically allowed but strongly discouraged because self is the convention.
Fix:
Use self as the first parameter name in class methods.
Practice the Transformation
A method is called as device.activate(mode). Identify the instance that Python passes automatically, then describe the equivalent class-method call using the class name Device.
Hints
- The instance is the object before the dot.
- Place that instance before mode in the class-method form.
- Use the pattern Class.method(instance, remaining arguments).
What do you think happens?
What is the equivalent class-method form of device.activate(mode)?
Reveal answer
Answer: Device.activate(device, mode)
The object before the dot, device, is automatically passed as the first argument. The explicitly supplied argument mode follows it.
Key Takeaways
- self is the first parameter in every class method and refers to the instance itself.
- Python automatically passes the instance when you call a method through an object.
- The instance method call myobject.method(arg1, arg2) corresponds to MyClass.method(myobject, arg1, arg2).
- The instance occupies the first parameter position, before the other arguments.
- Use self by convention; although other names are technically allowed, they are strongly discouraged.
Key Takeaways
- self represents the particular instance whose method is being called.
- Python supplies that instance automatically as the method's first argument.
- An instance call can be understood as a class-method call with the instance inserted first.
- The name self is a strong convention that makes the instance parameter clear to readers.