At Thursday 5/10/2006 13:53, [EMAIL PROTECTED] 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
--
http://mail.python.org/mailman/listinfo/python-list