Python (and me) getting confused finding keys
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Tue Dec 22 12:15:42 EST 2009
En Tue, 22 Dec 2009 13:56:36 -0300, John <john at nurfuerspam.de> escribió:
>> another thread can remove the key prior to the has_key call; or perhaps
>> edges isn't a real dictionary?
>>
>
> of course. But unless there is a way of using threading without being
> aware of
> it, this is not the case. Also, edges is definitely a dict (has been
> declared
> some lines before, and no obscure functions have been called in the
> meantime,
> just some adding to edges). Python would also fail on edges.keys() if
> edges
> wasn't a dict...
Ok, then your edges are mutable:
py> class Edge:
... def __init__(self, x):
... self.x = x
... def __eq__(self, other):
... return self.x==other.x
... def __hash__(self):
... return hash(self.x)
...
py> e1 = Edge(1)
py> e2 = Edge(2)
py> e3 = Edge(3)
py> edges = {e1:None, e2:None, e3:None}
py> e2.x = 5
py> for e in edges.keys():
... assert edges.has_key(e)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
AssertionError
py> for e in edges:
... assert e in edges
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
AssertionError
Once you compute an object's hash, it must remain immutable. Is this your
problem?
--
Gabriel Genellina
More information about the Python-list
mailing list