Python中有自动gc,这个gc在一般情况下也可以清除循环引用的对象。
不过有个例外的情况:一个对象显式定义了__del__方法。
例如如下的代码:
#!/usr/bin/env python
class Foo:
def __init__(self):
self._bar = {"test": self.test}
print "construct"
def test(self):
print "test"
def __del__(self):
print "del"
f = Foo()
del f
运行结果是不会打印”del”的。
gc.garbage
A list of objects which the collector found to be unreachable but could not be freed (uncollectable objects). By default, this list contains only objects with __del__() methods. [1] Objects that have __del__() methods and are part of a reference cycle cause the entire reference cycle to be uncollectable, including objects not necessarily in the cycle but reachable only from it. Python doesn’t collect such cycles automatically because, in general, it isn’t possible for Pyth