python lambda 表达式引起循环引用的问题
# encoding: utf-8
class A():
def run(self):
print "hello"
class B():
def __init__(self, call):
self._call = call
class Test():
def __init__(self):
self._a = A()
#self._b = B(self._a.run) # 使用这句代替lambda表达式可以输出"del Test"
tmp = self._a.run
self._b = B(lambda : tmp())
def __del__(self):
print "del Test"
t = Test()
t = None
t._b._call(t)
t._b._call()
self._b = B(lambda self: self._a.run())
经测试可以输出 del Test
def __init__(self):
self._a = A()
# self._b = B(self._a.run) # 使用这句代替lambda表达式可以输出"del Test"
self._b = B(lambda : self._a.run())
self._b._call() #这里才会进行函数调用
t = Test()
t._b._call =None #这里也置空就会输出del Test
t = None