Class Definition and Instance Methods
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 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
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.
- 1Identify the instance
myobject is identified as an instance of MyClass.
- 2Find the method
The call uses method associated with MyClass.
- 3Map the object
The object before the dot, myobject, is supplied as the value for self.
- 4Map the written arguments
arg1 and arg2 remain the explicitly supplied arguments.
- 5Rewrite 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
Common mistake Thinking that self must be written as an argument in the instance form.
What it looks likeTreating myobject.method(arg1, arg2) as though it needs another written value for self.
Why it failsThe instance call automatically supplies myobject as the value for self.
Do this insteadSeparate the automatic object from the explicit arguments: myobject supplies self, while arg1 and arg2 are written by the caller.
Common mistake Forgetting that the object before the dot matters.
What it looks likeReading method(arg1, arg2) without identifying myobject.Why it failsThe source conversion depends on the object used to make the call.
Do this insteadRewrite the call with its object visible: MyClass.method(myobject, arg1, arg2).
Common mistake Treating the class-level form and instance form as unrelated operations.
What it looks likeAssuming myobject.method(arg1, arg2) names a different method from MyClass.method(myobject, arg1, arg2).
Why it failsThe source describes the class-level form as the automatic conversion of the instance-level form.
Do this insteadUse 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
- 1An instance method is used through an instance such as myobject.
- 2In
myobject.method(arg1, arg2), myobject is automatically supplied as the value for self. - 3The equivalent class-level form is
MyClass.method(myobject, arg1, arg2). - 4The explicit arguments are the values written inside the parentheses; self comes from the object before the dot.
- 5When inheritance is involved, the source states that a base class's
__init__method must be called explicitly.
At a glance
- The object before the dot supplies the value for self.
myobject.method(arg1, arg2)corresponds toMyClass.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.