I'm trying to change the method of class on the fly. I can change it using the class name, like here:
class Foo:
def __init__(self, x):
self.x = x
def method(self):
print("original method")
return self.x
def new_method(self):
print("new method")
return self.x
Foo.method = new_method
foo = Foo(1)
print(foo.method()) # Works fine
But I'd like to change the method using the object name, not the class, and it raises an error:
foo = Foo(1)
foo.method = new_method
print(foo.method()) # TypeError: new_method() missing 1 required positional argument: 'self'
Would appreciate any help on this matter