用于使用弱引用存储对象的weakset类。
weakrefset的Python项目详细描述
python 2.7&3.1包含一个WeakSet类,一个用于存储对象的集合 使用弱引用 (见Python weakref module)。
此项目是用于Python2.5的weakrefset模块和测试的后台端口 和2.6。测试 需要unittest2 package。
- Mercurial存储库和问题跟踪程序:http://code.google.com/p/weakrefset/
您可以使用pip install weakrefset
安装它用法很简单。你不需要处理弱引用 他们自己,这个坏蛋 引用,您可以直接在对象上添加/删除/迭代:
>>> from weakrefset import WeakSet >>> class Foo(object): pass ... >>> f1 = Foo() >>> f2 = Foo() >>> refs = WeakSet([f1, f2]) >>> f3 = Foo() >>> refs.add(f3) >>> refs.remove(f1) >>> del f2 >>> f3 in refs True >>> for f in refs: ... print f ... <__main__.Foo object at 0x40e410>
要与Python2.5、2.6和未来版本的Python兼容,您可以 按如下方式导入:
try: # location in Python 2.7 and 3.1 from weakref import WeakSet except ImportError: # separately installed from weakrefset import WeakSet