[Python-porting] Control of hash randomization
martin at v.loewis.de
martin at v.loewis.de
Mon May 28 00:19:54 CEST 2012
Zitat von Aaron Meurer <asmeurer at gmail.com>:
> On Sun, May 27, 2012 at 3:03 PM, Antoine Pitrou <solipsis at pitrou.net> wrote:
>> 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.
>
> Yes, that was a simplification of the problem. I think when I saw it
> some very subtle thing was happening to the set. I can look it up if
> anyone's interested.
I was puzzled by your original claim as well. If you "admit" that the
set might have been touched somehow, I can readily believe that it gives
elements in a different order.
Regards,
Martin
More information about the Python-porting
mailing list