def funcA():
print("No")
def funcB():
print("Yes")
class A:
def __init__(self, func):
self.func = func
def getB(self):
b = B(self.func)
return b
def setfunc(self, func):
self.func = func
class B:
def __init__(self, func):
self.func = func
def execfunc(self):
self.func()
a = A(funcA)
b = a.getB()
a.setfunc(funcB)
b.execfunc()
- I create A and set A.func to be funcA (print "No")
- I create B and set B.func = A.func = funcA (print "No")
- I change A.func to be funcB (print "Yes")
- I execute B.func and it executes funcA (print "No")
If in step 2 the function was passed by reference, and the function target was changed in step 3, why does B.func not change to the new target?