如何在 Python 中创建对对象的弱引用?
Tim
问问题
835 次
1 回答
13
>>> 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())
有关更多详细信息,请参阅wearkref 模块文档。您还可以使用它weakref.proxy
来创建代理 o 的对象。ReferenceError
如果不再引用所指对象时使用,将抛出。
于 2008-09-08T23:13:16.527 回答