[Python-porting] Control of hash randomization

Antoine Pitrou solipsis at pitrou.net
Sun May 27 23:03:07 CEST 2012


Aaron Meurer <asmeurer at ...> writes:
> 
> It doesn't help that quite a few Python programmers don't understand
> just what is and is not guaranteed by hash dependent objects.  For
> example, I've seen this mistake made several times:
> 
> a = set(whatever) # or dict
> b = list(a)
> c = list(a)
> assert b == c
> 
> The assertion does NOT have to hold, and I've seen situations where it
> doesn't.

Actually, it should hold, since the iteration order of a given set or dict 
doesn't change (similarly, dict.keys(), dict.values() and dict.items() should 
all iterate in the same order). However, constructing the set in different ways 
may lead to different iteration orders.

Regards

Antoine.




More information about the Python-porting mailing list