Dictionary order (Is it consistent up to py3.3 unless using -R or PYTHONHASHSEED is set)

Steve D'Aprano steve+python at pearwood.info
Sun May 28 09:51:05 EDT 2017


On Sun, 28 May 2017 11:12 pm, Jon Ribbens wrote:

> On 2017-05-28, Bill Deegan <bill at baddogconsulting.com> wrote:
>> As a follow up to a discussion on IRC #python channel today.
>>
>> Assuming the same order of insertions of the same items to a dictionary
>> would the iteration of a dictionary be the same (not as the order of
>> insertion, just from run to run) for Python 2.7 up to python 3.3? And again
>> at Python 3.6?
>> (Also for py 3.3 through 3.5.x if PYTHONHASHSEED=0)
>>
>> With python 3.6 having the added benefit of guaranteeing insertion order is
>> maintained?
> 
> I think Steve missed the point of what you were asking, as well as
> being wrong about dict order in 3.6. Here's the official line on that:

Thanks for quoting the official line, because it agrees with what I said: dict
order in 3.6 is considered an implementation detail, not a language guarantee,
and shouldn't be relied on (except for function **kwargs).

What exactly did you think I got wrong?

- that **kwargs preserve order?

- that Python 3.6 does NOT guarantee that other dicts preserve order?

- that Python may make that promise in the future?

Everything I said is supported by the very document you quoted.


>     The order-preserving aspect of this new implementation is considered
>     an implementation detail and should not be relied upon (this may
>     change in the future, but it is desired to have this new dict
>     implementation in the language for a few releases before changing the
>     language spec to mandate order-preserving semantics for all current
>     and future Python implementations
> 
> ( https://docs.python.org/3/whatsnew/3.6.html#whatsnew36-compactdict )
> 
> If you're asking "given a fixed Python version, and where appropriate
> PYTHONHASHSEED=0, will dictionaries created the same way always
> iterate in the same order"

Why on earth would you assume a fixed Python version when the OP *explicitly*
asks about Python 2.7 through 3.3? That's two major versions, four minor
versions (excluding 3.0, which nobody should use), and who knows how many bug
fix versions. Since the iteration order is explicitly subject to change, then
it is possible for any release (major, minor or bug fix) to change the
iteration order.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list