[Python-Dev] set iteration order

Steven D'Aprano steve at pearwood.info
Sun Feb 27 01:06:04 CET 2011


Hagen Fürstenau wrote:
>> Code with any dependence on the iteration order of unordered collections
>> (other than the guarantee that d.keys() and d.values() match at any
>> given time as long as d is unchanged) is buggy.
> 
> It's not a matter of dependence on iteration order, but of
> reproducibility (in my case there were minor numerical differences due
> to different iteration orders). 

If those differences are insignificant to you, then why do you care?

If they are significant enough that (say) tests were failing, then your 
results depend on the iteration order of a set, and your code is buggy 
and should be fixed. Or perhaps your tests are too strict.


> I think we also warn about changes in
> pseudorandom number sequences, although you could argue that no code
> should depend on specific pseudorandom numbers.

The random module provides an API for repeating sequences of 
pseudorandom numbers: the seed. So you *can* depend on specific numbers, 
if you need to.

Sets and dicts do not provide any such API. The order even changes with 
the history of the object: two equal sets can have different iteration 
orders.

Personally, I don't care whether or not we mention that set iteration 
order has changed. It seems too trivial to worry much about it.

+0



-- 
Steven



More information about the Python-Dev mailing list