If one changes a object attribute which refers to an internal method to an external function after the object is created, self is not passed to the new function, even if the attribute (which contains a function) is called like before.
class Person:
def greet(self):
print("hello")
do = greet
def wave(person):
print("bye")
alice = Person()
alice.do() #prints 'hello'
#change do to point an external function
alice.do = wave
alice.do() #Error: Missing argument
The exact error which I get is:
hello
Traceback (most recent call last):
File "C:\Users\Owner\Desktop\miniLiveMethodSwitch.py", line 15, in <module>
alice.do() #Error: Missing argument
TypeError: wave() missing 1 required positional argument: 'person'
If one move the external function into the class as a internal method,
class Person:
def greet(self):
print("hello")
def wave(person):
print("bye")
do = greet
Then the code works as expected. Why isn't self passed once the attribute is changed to refer to an external function? What is the proper way to call the function so that self is passed?