Problem with OrderedDict - progress report

Chris Angelico rosuav at gmail.com
Fri Jun 1 04:01:25 EDT 2018


On Fri, Jun 1, 2018 at 5:54 PM, Gregory Ewing
<greg.ewing at canterbury.ac.nz> wrote:
> Chris Angelico wrote:
>>
>> It is an error to mutate the dictionary *and then continue to iterate over
>> it*.
>
>
> But if you're processing the last key, and you add one so
> that it's no longer the last key, what should happen?
>
> My feeling is that this should be an error, because it's
> not clear whether iteration should stop at that point
> or not.
>

Right, but if you 'break' immediately after a mutation, that isn't
continuing to iterate. Even though you're inside the loop, there's no
further action taken to process the loop, and no problem.

If it so happens that mutating the last one and then continuing the
loop doesn't cause a problem, it's because you got lucky, not because
the language guarantees it.

ChrisA



More information about the Python-list mailing list