A WeakList class for storing objects using weak references in a list
Table of Contents
Install it from PyPi:
pip install weakreflist
or from Github:
git clone firstname.lastname@example.org:apieum/weakreflist.git cd weakreflist python setup.py install
WeakList provides the same methods as the built-in
list but will remove any
weakref-compatible objects that
are released by the garbage collector.
Example for CPython:
from weakreflist import WeakList class A(object): """weakrefs don't function directly on object()""" objectA = A() my_list = WeakList([objectA]) assert len(my_list) == 1 del objectA assert len(my_list) == 0 # objectA removed from list
Pypy has a different implementation of garbage collection which changes some behavior of
weakref. This may be true in other 3rd-party compilers such as Jython and Cython. Some interactive interpreters, like IPython, may also delay calls to
Due to this, you will need to explicitly call
gc.collect()which has a negative impact on performance!
Example for other Python implementations
from weakreflist import WeakList import gc class A(object): """weakrefs don't function directly on object()""" objectA = A() my_list = WeakList([objectA]) assert len(my_list) == 1 del objectA assert len(my_list) == 1 # gc did not run gc.collect() # must be called explicitly assert len(my_list) == 0
Your feedback, code review, improvements, bug reports, and help to document is appreciated. You can contact me by email: apieum [at] gmail [dot] com
Install recommended packages for running tests:
pip install -r dev-requirements.txt
Sometimes --spec-color doesn't function. This can potentially be fixed by the following:
Be certain to reinstall them separately and in the order given, otherwise you will continue to experience the same issue!
git clone email@example.com:apieum/weakreflist.git cd weakreflist nosetests --with-spec --spec-color ./weakreflist # or with watch # nosetests --with-spec --spec-color --with-watch ./weakreflist
- Thanks to BoonsNaibot for the following contributions:
- extended slicing support
- __reversed__, count, extend, and insert methods.