[Python-ideas] RFC: PEP: Add dict.__version__

Nick Coghlan ncoghlan at gmail.com
Sat Jan 9 08:12:49 EST 2016
On 9 January 2016 at 19:18, Victor Stinner <victor.stinner at gmail.com> wrote:
> It would be nice to detect keys mutation while iteration on
> dict.keys(), but it would also be be nice to detect values mutation
> while iterating on dict.values() and dict.items(). No?

No, because mutating values as you go while iterating over a
dictionary is perfectly legal:

>>> data = dict.fromkeys(range(5))
>>> for k in data:
...     data[k] = k
...
>>> for k, v in data.items():
...     data[k] = v ** 2
...
>>> data
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

It's only changing the key in the dict that's problematic, as that's
the one that can affect the iteration order, regardless of whether
you're emitting keys, values, or both.

Raymond did mention that when closing the issue, but it was as an
aside in one of his bullet points, rather than as a full example.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list