I think my example will make this easier to follow:
class x():
def a(self):
return "hello"
def b(self):
return self.a() + " world"
test = x()
print test.b() # prints "hello world" as expected
test.a = lambda(self): "hola"
print test.b() # throws error:
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# File "<stdin>", line 5, in b
# TypeError: <lambda>() takes exactly 1 argument (0 given)
Attempting to update to point x().a to another function, but when x().b calls it, it doesn’t appear to pass self as the first argument.
I expected to get “hola world”.
You can see the problem if you do
If you really want to patch
aonly ontest(not on all instances ofx), you can do:To create an object of type
instancemethod.