weakSet

Tim Peters tim.peters at gmail.com
Thu Oct 5 13:29:50 EDT 2006


[jean.philippe.mague at gmail.com]
> Has anyone ever think about a set wich references its elements weakly ?

Yes, and there are excruciating subtleties.  I only implemented as
much of one as ZODB needed at the time:

# A simple implementation of weak sets, supplying just enough of Python's
# sets.Set interface for our needs.

class WeakSet(object):
    """A set of objects that doesn't keep its elements alive.

    The objects in the set must be weakly referencable.
    The objects need not be hashable, and need not support comparison.
    Two objects are considered to be the same iff their id()s are equal.

    When the only references to an object are weak references (including
    those from WeakSets), the object can be garbage-collected, and
    will vanish from any WeakSets it may be a member of at that time.
    """

For the rest ;-), including discussion of excruciating subtleties that
arise even implementing this little, see:

    http://svn.zope.org/ZODB/trunk/src/ZODB/utils.py



More information about the Python-list mailing list