Persistent sets

Skip Montanaro skip at
Fri Jun 6 14:06:23 CEST 2003

Following up my own post...  I later realized I had to be careful not to
wipe out the persistent set's _data attribute because it might then become
non-persistent.  A quick scan of revealed that the Set.__iand__
method needed to be changed.

    import sets
    import shelve

    class PersistentSet(sets.Set):
        def __init__(self, file, iterable=None):
            self._data =
            if iterable is not None:

        def __iand__(self, other):
            """Update a set with the intersection of itself and another."""
            ia = (self & other)._data
            return self

Again, no tests yet.  Maybe I'll get around to adapting some of the tests
from the sets module unit tests...


More information about the Python-list mailing list