weakSet

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jean.philippe.mague@gmail.com

    #1

    weakSet

    Hello,

    Has anyone ever think about a set wich references its elements weakly ?
    The *easy* solution would provide a WeakSet class with the following
    behavior:
    >>>s=set([a, b])
    >>>ws=WeakSet ([b,c])
    >>>(ws&s).__cla ss__() is WeakSet
    True
    >>>s&ws
    TypeError: unsupported operand type(s) for &: 'set' and 'instance'

    the hard olution would add the following behavior:
    >>>(s&ws).__cla ss__() is WeakSet
    True

    Any hint for the easy solution ?
    for the hard one ?

    Jean-Philippe

  • Tim Peters

    #2
    Re: weakSet

    [jean.philippe.m ague@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:


    Comment

    • Gabriel Genellina

      #3
      Re: weakSet

      At Thursday 5/10/2006 13:53, jean.philippe.m ague@gmail.com wrote:
      >Has anyone ever think about a set wich references its elements weakly ?
      >The *easy* solution would provide a WeakSet class with the following
      >behavior:
      >>s=set([a, b])
      >>ws=WeakSet([b,c])
      >>(ws&s).__clas s__() is WeakSet
      >True
      >>s&ws
      >TypeError: unsupported operand type(s) for &: 'set' and 'instance'
      >
      >the hard olution would add the following behavior:
      >>(s&ws).__clas s__() is WeakSet
      >True
      >
      >Any hint for the easy solution ?
      >for the hard one ?
      For s&ws you can define the reversed operators, __rand__ in this case
      (look up in the Python Reference Manual)
      >>>
      >>class A(object): pass
      ....
      >>a=A()
      >>1&a
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      TypeError: unsupported operand type(s) for &: 'int' and 'A'
      >>class B(object):
      .... def __init__(self, value): self.value = value
      .... def __repr__(self): return 'B(%s)' % self.value
      .... def __rand__(self, other):
      .... return B(self.value&ot her)
      ....
      >>b=B(3)
      >>b
      B(3)
      >>1&b
      B(1)
      >>>
      Now, managing the weakrefs is up to you...


      Gabriel Genellina
      Softlab SRL





      _______________ _______________ _______________ _____
      Preguntá. Respondé. Descubrí.
      Todo lo que querías saber, y lo que ni imaginabas,
      está en Yahoo! Respuestas (Beta).
      ¡Probalo ya!


      Comment

      Working...