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).
- 1Start with the instance call
The call begins with myobject before the dot, followed by method and the arguments arg1 and arg2.
- 2Place the instance first
Python uses myobject as the first argument supplied to the method.
- 3Keep the remaining arguments in order
arg1 and arg2 remain after the instance.
- 4Read 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 |
The conventional name self is strongly preferred even though another name is technically allowed.
Mistakes with Method Calls
Common mistake Treating self as an argument that must be written in the ordinary object call.
What it looks likeTrying to add self explicitly when calling myobject.method(arg1).
Why it failsPython automatically passes the instance to the method.
Do this insteadCall the method through the object without explicitly supplying self.
Common mistake Forgetting that the instance occupies the first parameter position.
What it looks likeReading myobject.method(arg1, arg2) as though arg1 were the first value received by the method.
Why it failsThe object before the dot is passed first, and the remaining arguments follow it.
Do this insteadTrace the equivalent form MyClass.method(myobject, arg1, arg2).Common mistake Replacing self with a different parameter name without a strong reason.
What it looks likeUsing another name for the first parameter and expecting readers to recognize the instance immediately.
Why it failsOther names are technically allowed but strongly discouraged because self is the convention.
Do this insteadUse 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
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
- 1self is the first parameter in every class method and refers to the instance itself.
- 2Python automatically passes the instance when you call a method through an object.
- 3The instance method call
myobject.method(arg1, arg2)corresponds toMyClass.method(myobject, arg1, arg2). - 4The instance occupies the first parameter position, before the other arguments.
- 5Use self by convention; although other names are technically allowed, they are strongly discouraged.
At a glance
- 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.