Sub topic 8
How Functions Become Bound Methods
This chapter assumes you know how obj.name searches the instance __dict__ and the class, and that you have seen the descriptor protocol (__get__, __set__). Here the protocol explains something you use every day: why self is filled in automatically.
A function is a descriptor
Every def inside a class body creates an ordinary function object. The function type defines __get__ but not __set__. That makes functions non-data descriptors.
When you write c.greet, object.__getattribute__ finds greet in the class, sees that its type has __get__, and calls it with the instance and the class. The function's __get__ returns a bound method. The bound method stores the instance and the original function.
class C:
def greet(self):
return "hi"
c = C()
plain = C.__dict__["greet"]
bound = plain.__get__(c, C)
print(type(plain).__name__) # function
print(type(bound).__name__) # method
print(bound() == c.greet()) # True
print(bound == c.greet) # TrueReading from C.__dict__ bypasses the descriptor call, so you get the raw function. Calling __get__ by hand does what c.greet does behind the scenes.
What the bound method holds
A bound method is a small wrapper with two attributes. __self__ is the instance, and __func__ is the original function. Calling it runs __func__(__self__, *args).
class C:
def greet(self):
return "hi"
c = C()
m = c.greet
print(m.__self__ is c) # True
print(m.__func__ is C.greet) # TrueC.greet is the plain function here. When __get__ receives None as the instance, the function returns itself. That is why C.greet(c) works: you supply self yourself.
Why instances can override methods
Because functions have no __set__, they are non-data descriptors. An entry of the same name in the instance __dict__ wins over them.
class C:
def greet(self):
return "hi"
c = C()
c.greet = lambda: "shadowed"
print(c.greet()) # shadowed
print(C().greet()) # hiThe lambda is stored as-is in c.__dict__. Lookup finds it there first and returns it without calling __get__, so it is not bound. That is why it takes no self. This is a common source of confusion when you patch a method on a single instance. To give an instance a real bound method, build one with types.MethodType(func, instance).
Common mistakes
- Assigning a function to an instance and expecting it to receive
self. Only functions found on the class are bound. - Comparing bound methods with
is. Each access toc.greetcreates a new method object, soc.greet is c.greetisFalse. Use==. - Storing a callable object without
__get__in a class and expecting binding. A class instance with only__call__is not bound, so it never receivesself.
Summary
- Functions defined in a class are non-data descriptors.
function.__get__(obj, cls)returns a bound method that remembersobj.function.__get__(None, cls)returns the function unchanged.- Instance attributes shadow methods because functions lack
__set__.
Exercise
Given class K: def f(self): return 1 and k = K(), what do K.f is K.__dict__["f"] and k.f is k.f evaluate to, and why?
Show answer
The first is True, because accessing a function through the class calls __get__(None, K), which returns the function itself. The second is False, because each access to k.f calls __get__ again and builds a new bound method object.
Exercise
After k.f = lambda: 2, what does k.f() return, and why does the lambda take no self?
Show answer
It returns 2. Functions are non-data descriptors, so the instance __dict__ entry wins, and lookup returns the lambda without calling __get__. It is never bound, so it receives no self.