Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python (and me) getting confused finding keys

Reply
Thread Tools

Python (and me) getting confused finding keys

 
 
John
Guest
Posts: n/a
 
      12-22-2009
Hi there,

I have a rather lengthy program that troubles me for quite some time. After
some debugging, I arrived at the following assertion error:

for e in edges.keys():
assert edges.has_key(e)

Oops!? Is there ANY way that something like this can possibly happen?

Cheers,
John
 
Reply With Quote
 
 
 
 
Lie Ryan
Guest
Posts: n/a
 
      12-22-2009
On 12/23/2009 3:33 AM, John wrote:
> Hi there,
>
> I have a rather lengthy program that troubles me for quite some time. After
> some debugging, I arrived at the following assertion error:
>
> for e in edges.keys():
> assert edges.has_key(e)
>
> Oops!? Is there ANY way that something like this can possibly happen?


in a multithreaded program, it's possible another thread erased 'e'
after the for-loop grabbed it but before the suite is executed.

but often you'll get something like this:
RuntimeError: dictionary changed size during iteration
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-22-2009
On Tue, 22 Dec 2009 17:33:04 +0100, John wrote:

> Hi there,
>
> I have a rather lengthy program that troubles me for quite some time.
> After some debugging, I arrived at the following assertion error:
>
> for e in edges.keys():
> assert edges.has_key(e)
>
> Oops!? Is there ANY way that something like this can possibly happen?


In another post, you assert that:

(1) You aren't knowingly using threads, so it's not likely that another
thread is modifying edges.

(2) edges is a regular dictionary, not a custom mapping class.


In that case, I would say that the most likely culprit is that the edges
are mutable but given a hash function. I can reproduce the problem like
this:


>>> class Edge:

.... def __init__(self, start, finish):
.... self.ends = (start, finish)
.... def __eq__(self, other):
.... return self.ends == other.ends
.... def __hash__(self):
.... return hash(self.ends)
....
>>> edges = {Edge(1, 5): None}
>>> for e in edges:

.... assert e in edges # same as edges.has_key(e)
.... e.ends = (5, 6)
.... assert e in edges, "and now it's gone"
....
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
AssertionError: and now it's gone



So the likely problem is that your edge type is mutable and being mutated.

Now, if your code snippet above:

for e in edges.keys():
assert edges.has_key(e)


is a literal copy-and-paste from the failing code, I can only assume that
the edge type __eq__ or __hash__ method mutates self.

The lesson of this? Do not make mutable classes hashable.

The obvious follow-up is to ask how to make an immutable class.

http://northernplanets.blogspot.com/...in-python.html



--
Steven
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-23-2009
* Steven D'Aprano:
>

[snip]
> The obvious follow-up is to ask how to make an immutable class.
>
> http://northernplanets.blogspot.com/...in-python.html


Thanks, I've been wondering about that.

By the way, the link at the bottom in the article you linked to, referring to an
earlier posting by Alex Martelli, was broken.

I believe it was the posting available here: <url:
http://www.opensubscriber.com/message/(E-Mail Removed)/2659890.html>.


Cheers,

- Alf
 
Reply With Quote
 
John
Guest
Posts: n/a
 
      12-23-2009

> The lesson of this? Do not make mutable classes hashable.


That could be it! I'll try. Thanks a lot!

> The obvious follow-up is to ask how to make an immutable class.
>
> http://northernplanets.blogspot.com/...es-in-python.h
> tml
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Confused about wireless passphrases vs. keys mike Wireless Networking 0 04-23-2011 02:16 PM
How to link foreign keys & primary keys using python? Harry George Python 9 06-13-2006 06:29 AM
can std::set hold pointers to keys instead of the keys themselves? danibe@my-deja.com C++ 10 02-03-2006 03:44 AM
serial keys/validation keys sandeep Kanwal C++ 1 10-29-2004 06:36 PM
keyboard keys replacing mouse keys? larry Computer Support 8 09-14-2003 07:32 PM



Advertisments