Class Definition and Instantiation
The self parameter is the first parameter in every class method and refers to the instance itself.
The Hidden First Argument
When you call a method through an object, Python supplies that object to the method automatically. The method receives the object through its first parameter. By convention, that parameter is named self.
self refers to the particular instance on which the method was called. It is not an extra value that you normally type in the method call.
Following the Call
Consider the instance method call myobject.method(arg1, arg2). Python treats the call as the class method call MyClass.method(myobject, arg1, arg2). The instance moves into the first parameter position, while the other arguments keep their order.
Reading the First Argument
Trace the transformation of myobject.method(arg1, arg2).
Start with the instance call: The call begins with myobject before the dot, followed by the method arguments arg1 and arg2.
Move the instance into the first position: Python supplies myobject as the first argument to the method.
Preserve the remaining arguments: arg1 and arg2 remain after the instance, producing the class method form MyClass.method(myobject, arg1, arg2).
The first method parameter receives myobject. When that parameter follows the naming convention, it is called self.
What self Identifies
A class contains the method definition, while an instance is the particular object involved in a method call. When the method is called through that instance, the instance becomes the value received by self. Therefore, self identifies the object participating in this specific call, rather than some separate object created by the method.
The First Parameter Position
The important requirement is the first parameter position in the method definition: that position receives the instance. Python does not require the parameter to be spelled self, but self is the established convention and should always be used. Other names are technically allowed, yet strongly discouraged because they make the method less immediately recognizable to other Python programmers.
The name self communicates the role of the first parameter, but the position is what allows it to receive the instance in the transformation.
Mistakes with self
Trying to pass self explicitly in an instance method call.
Python automatically passes the instance when the method is called through that instance.
Fix:
Call the method with only the ordinary method arguments; Python supplies the instance as the first argument.Thinking self refers to the class in every situation.
self refers to the instance itself.
Fix:
Identify the object before the dot in the instance call. That object is the value received by self.Using a different name for the first parameter without a strong reason.
Other names are technically allowed, but the convention makes the method's instance parameter clear to readers.
Fix:
Use self consistently as the first parameter name.
Use self as the first parameter of every class method. This follows the convention, makes the instance connection visible, and helps readers understand the method call transformation.
Trace It Yourself
A method is called as object_a.process(value). Describe the equivalent class method form. Then identify which value becomes self and explain whether the caller should type self explicitly.
Hints
- Place the instance as the first argument in the class method form.
- The instance is the object before the dot.
- The caller does not pass self explicitly.
What do you think happens?
What is the equivalent class method form of object_a.process(value)?
Reveal answer
Answer: MyClass.process(object_a, value)
Python places the instance object_a first, followed by the ordinary argument value. The caller does not add self separately.
Key Takeaways
- self is the first parameter in a class method and refers to the instance itself.
- When a method is called through an instance, Python automatically passes that instance as the first argument.
- The instance call myobject.method(arg1, arg2) corresponds to MyClass.method(myobject, arg1, arg2).
- The name self is a convention rather than the only technically permitted name, but it should be used.
- The caller supplies ordinary method arguments, not self explicitly.
Key Takeaways
- self identifies the particular instance involved in a method call.
- Python automatically passes that instance into the method's first parameter.
- An instance method call can be understood as a class method call with the instance inserted first.
- self is the strongly recommended conventional name for that first parameter.
- A caller never passes self explicitly when calling the method through an instance.