Oct. 7, 2020
5:08 a.m.
In Python 3 you can do this without any decorators. The following works and produces the same output: class AA: def greet(A_instance): print("Hello", A_instance.name) class BB: def greet(A_instance): print("Hi", A_instance.name) class A: def __init__(self, state, name): self.state = state self.name = name def greet(self): self.state.greet(self) def switchAA(self): self.state = AA def switchBB(self): self.state = BB if __name__ == "__main__": obj = A(AA, "alperen") obj.greet() obj.switchBB() obj.greet() -- Greg