News for hackers. Stuff that matters.
Re:Migrating to Python 3 with pleasure by StavrosK on Monday February 05, 2018 @03:12AM (#16307368)
No, of course not. Some order has to be applied to serialize it to a string, but that order is not defined between calls.
Re:Migrating to Python 3 with pleasure by eesmith on Monday February 05, 2018 @03:31AM (#16307444)
Python long ago made the guarantee that:

> Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions. If keys, values and items views are iterated over with no intervening modifications to the dictionary, the order of items will directly correspond.

That is, the order will be maintained between calls, so long as the dictionary had not been modified.

Going back to "str(some_dict) == str(some_dict)". I would not expect to always be the same, but for entirely different reasons. Consider:

  class Strange():
    def __repr__(self):
      import random
      return str(random.random())

  some_dict = {1: Strange()}

  >>> str(some_dict) == str(some_dict)
  False
Re:Migrating to Python 3 with pleasure by StavrosK on Monday February 05, 2018 @03:44AM (#16307499)
A section called "CPython implementation detail" looks like the very antithesis of a guarantee, to me.
Re:Migrating to Python 3 with pleasure by eesmith on Monday February 05, 2018 @03:51AM (#16307529)
My apologies. I looked for the answer I knew was there, and quoted the wrong section because it matched what I was looking for.

I should have quoted the next section:

> If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond.

Curiously, the "Dictionary view objects" section at https://docs.python.org/2.7/library/stdtypes.html#dictionary... has the same "Keys and values are iterated over in an arbitrary order which is non-random ..." text, but without being inside of a "CPython implementation detail" box.

Re:Migrating to Python 3 with pleasure by StavrosK on Monday February 05, 2018 @04:53AM (#16307766)
Huh, that's inconsistent. I guess they changed it to avoid the security problem and forgot to change the docs? Or maybe they didn't update 2.7 at all and it still behaves that way.