Class Definition and Instance Methods
You must be wondering how Python gives the value for self and why you don't need to give a value for it. An example will make this clear. Say you have a class called MyClass and an instance of this class called myobject . When you call a method of this object as myobject.method(arg1, arg2) , this is automatically converted by Python into MyClass.method(myobject, arg1, arg2) - this is all the special self is about.
The Hidden Argument
An instance method call can look as if it receives only the arguments written inside its parentheses. The important question is: where does the value for self come from? If myobject is an instance of MyClass, the call myobject.method(arg1, arg2) is automatically converted by Python into MyClass.method(myobject, arg1, arg2). The object before the dot supplies the value that appears in the self position.
What do you think happens?
Suppose myobject is an instance of MyClass. What class-level call corresponds to myobject.method(arg1, arg2)?
Reveal answer
Answer: MyClass.method(myobject, arg1, arg2)
The instance named before the dot is automatically supplied as the first value in the class-level form. That value is received in the method's self position.
Class and Instance Roles
A class and an instance have different roles in this relationship. MyClass is the class associated with the method. myobject is an instance of that class. When the method is used through myobject, Python connects that call to MyClass.method and supplies myobject as the value for self. This is why the method can be understood as operating in the context of that particular object.
Argument Mapping
self is the value supplied by the object used to make the instance method call. In myobject.method(arg1, arg2), myobject supplies self, while arg1 and arg2 are the arguments written explicitly. The equivalent class-level form shows this mapping directly: MyClass.method(myobject, arg1, arg2).
| Part of the call | How it is supplied | Role |
|---|---|---|
| myobject | Automatically | Provides the value for self |
| arg1 | Explicitly | First written argument |
| arg2 | Explicitly | Second written argument |
The source model's argument mapping for myobject.method(arg1, arg2)
self During Execution
While the instance method is being used through myobject, self refers to that object because myobject was supplied in the self position. This gives the method an object-specific context. The name self is significant because it identifies the object that the method call is working with; it is not a value that you write inside the parentheses in the instance form.
A Complete Trace
Tracing one instance method call
Trace myobject.method(arg1, arg2) and identify what Python supplies automatically.
Identify the instance: myobject is identified as an instance of MyClass.
Find the method: The call uses method associated with MyClass.
Map the object: The object before the dot, myobject, is supplied as the value for self.
Map the written arguments: arg1 and arg2 remain the explicitly supplied arguments.
Rewrite the call: The instance form is represented as MyClass.method(myobject, arg1, arg2).
myobject supplies self automatically; arg1 and arg2 are supplied explicitly.
Common Calling Mistakes
Thinking that self must be written as an argument in the instance form.
The instance call automatically supplies myobject as the value for self.
Fix:
Separate the automatic object from the explicit arguments: myobject supplies self, while arg1 and arg2 are written by the caller.Forgetting that the object before the dot matters.
The source conversion depends on the object used to make the call.
Fix:
Rewrite the call with its object visible: MyClass.method(myobject, arg1, arg2).Treating the class-level form and instance form as unrelated operations.
The source describes the class-level form as the automatic conversion of the instance-level form.
Fix:
Use the two forms as two views of the same method call.
Inheritance Note
The same self idea matters when inheritance is involved. The source explains that base class names are specified in a tuple following the class name in the class definition. It also explains that the base class's __init__ method is explicitly called using self when the base-class part of an object must be initialized; Python does not automatically call that base-class constructor. Separately, instances of Teacher or Student can be treated as instances of SchoolMember when using the tell method of SchoolMember.
Practice Trace
For the call student.tell(subject, message), suppose student is an instance of Student and Student is related to SchoolMember as described in the source. Identify which value is supplied automatically as self and rewrite the call in the class-level form.
Hints
- Start with the object before the dot.
- Place that object in the self position.
- Keep subject and message as the explicitly supplied arguments.
The expected mapping is: student supplies self, while subject and message are explicit arguments. The corresponding class-level view is SchoolMember.tell(student, subject, message) when the tell method being used is the one provided by SchoolMember.
Key Takeaways
- An instance method is used through an instance such as myobject.
- In myobject.method(arg1, arg2), myobject is automatically supplied as the value for self.
- The equivalent class-level form is MyClass.method(myobject, arg1, arg2).
- The explicit arguments are the values written inside the parentheses; self comes from the object before the dot.
- When inheritance is involved, the source states that a base class's __init__ method must be called explicitly.
Key Takeaways
- The object before the dot supplies the value for self.
- myobject.method(arg1, arg2) corresponds to MyClass.method(myobject, arg1, arg2).
- self connects the method call to the particular instance that made the call.
- Arguments written in parentheses are explicit; the instance is supplied automatically.
- A base class's __init__ method is not automatically called according to the source; it must be called explicitly.