Method Objects
Usually, a method is called right after it is bound:
x.f()
If x = MyClass(), as above, this will return the string 'hello world'.
However, it is not necessary to call a method right away: x.f is a method
object, and can be stored away and called at a later time. For example:
xf = x.f
while True:
print(xf())
will continue to print hello world until the end of time.
What exactly happens when a method is called? You may have noticed that
x.f() was called without an argument above, even though the function
definition for f() specified an argument. What happened to the argument?
Surely Python raises an exception when a function that requires an argument is
called without any — even if the argument isn’t actually used…
Actually, you may have guessed the answer: the special thing about methods is
that the instance object is passed as the first argument of the function. In our
example, the call x.f() is exactly equivalent to MyClass.f(x). In
general, calling a method with a list of n arguments is equivalent to calling
the corresponding function with an argument list that is created by inserting
the method’s instance object before the first argument.
In general, methods work as follows. When a non-data attribute of an instance is referenced, the instance’s class is searched. If the name denotes a valid class attribute that is a function object, references to both the instance object and the function object are packed into a method object. When the method object is called with an argument list, a new argument list is constructed from the instance object and the argument list, and the function object is called with this new argument list.