By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

weakSet

P: n/a
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).__class__() is WeakSet
True
>>>s&ws
TypeError: unsupported operand type(s) for &: 'set' and 'instance'

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

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

Jean-Philippe

Oct 5 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
[je*****************@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
Oct 5 '06 #2

P: n/a
At Thursday 5/10/2006 13:53, je*****************@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).__class__() is WeakSet
True
>>s&ws
TypeError: unsupported operand type(s) for &: 'set' and 'instance'

the hard olution would add the following behavior:
>>(s&ws).__class__() 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&other)
....
>>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!
http://www.yahoo.com.ar/respuestas

Oct 5 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.