Why is this Python function not passed by reference?

Viewed 40
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()
  1. I create A and set A.func to be funcA (print "No")
  2. I create B and set B.func = A.func = funcA (print "No")
  3. I change A.func to be funcB (print "Yes")
  4. 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?

0 Answers
Related