Problem with OrderedDict - progress report

Chris Angelico rosuav at gmail.com
Thu May 31 13:35:04 EDT 2018


On Fri, Jun 1, 2018 at 12:37 AM, Frank Millman <frank at chagford.com> wrote:
> "Steven D'Aprano"  wrote in message news:peorib$1f4$2 at blaine.gmane.org...
>>
>>
>> On Thu, 31 May 2018 10:05:43 +0200, Frank Millman wrote:
>>
>> > From the interpreter session below, you will see that adding a key while
>> > processing the *last* key in an OrderedDict does not give rise to an
>> > exception.
>>
>> If you mutate the dict, and then stop iterating over it, there is no
>> check that the dict was mutated.
>>
>> It isn't an error to mutate the dict. It is an error to mutate it while
>> it is being iterated over. If you stop the iteration, there's no problem.
>>
>
> Agreed, but my gut feel, and the following example, suggest that when
> processing the last key in a dictionary while iterating over it, you have
> not yet stopped iterating.

If it's easier to understand, here's an alternative wording:

It is an error to mutate the dictionary *and then continue to iterate over it*.

ChrisA



More information about the Python-list mailing list