如何在Python中创建一个弱引用对象?
>>> import weakref
>>> class Object:
... pass
...
>>> o = Object()
>>> r = weakref.ref(o)
>>> # if the reference is still active, r() will be o, otherwise None
>>> do_something_with_o(r())
查看Weakref模块文档以获取更多详细信息。
您还可以使用weakref.proxy
来创建代理对象o。 如果在引用不再被引用时使用,将会抛出ReferenceError
。