Python标准库参考笔记-weakref
9.11 weakref
地址:http://docs.python.org/library/weakref.html
对一个对象的弱引用。相对于通常的引用来说,如果一个对象有一个常规的引用,它是不会被垃圾收集器销毁的,但是如果一个对象只剩下一个弱引用,那么它可能被垃圾收集器收回。
并非所有的对象都支持weakref,例如list和dict就不支持,但是文档中介绍了可以通过继承dict来支持weakref。
建立一个弱引用:
# -*- coding: cp936 -*-
import weakref
class TestObj:
pass
a = TestObj()
#建立一个a的弱引用
x = weakref.ref(a)
print x
print x()
结果:
>>>
<weakref at 01639630; to 'instance' at 01923D50>
<__main__.TestObj instance at 0x01923D50>
引用对象销毁时的回调函数
是在建立弱引用的时候指定一个回调函数,一旦自己引用的对象被销毁,将会调用这个回调函数。
# -*- coding: cp936 -*-
import weakref
class TestObj:
pass
def test_func(reference):
print 'Hello from Callback function!'
print reference, 'This weak reference is no longer valid'
a = TestObj()
#建立一个a的弱引用
x = weakref.ref(a, test_func)
del a
结果:
>>>
Hello from Callback function!
<weakref at 01639630; dead> This weak reference is no longer valid
在a被删除之后,由于TestObj的实例只剩下一个,所以被销毁,这时候指定的回调函数就会执行。
代理Proxy
使用代理和使用普通weakref的区别就是不需要(),可以像原对象一样地使用proxy访问原对象的属性。
# -*- coding: cp936 -*-
import weakref
class TestObj:
def __init__(self):
self.test_attr = 100
def test_func(reference):
print 'Hello from Callback function!'
a = TestObj()
#建立一个对a的代理(弱引用)
x = weakref.proxy(a, test_func)
print a.test_attr
print x.test_attr
del a
结果:
>>>
100
100
Hello from Callback function!
PyMOTW:
http://www.doughellmann.com/PyMOTW/weakref/index.html

871

被折叠的 条评论
为什么被折叠?



