如何在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())
有关更多详细信息,请参阅wearkref模块文档.您还可以使用weakref.proxy
创建代理o的对象.ReferenceError
如果在不再引用引用对象时使用将抛出.