Problem with OrderedDict - progress report

Frank Millman frank at chagford.com
Fri Jun 1 04:04:30 EDT 2018


"Gregory Ewing"  wrote in message news:fnccd8Ff3s1U1 at mid.individual.net...
>
> 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.
>

I agree. A normal dictionary raises an exception. I think that an 
OrderedDict should do so as well.

In fact this was the reason for my intermittent error. I was adding a key 
while iterating, which was an error. But if I happened to be on the last key 
at the time, it did not raise an exception. If I was on a previous key, it 
did raise an exception.

Frank






More information about the Python-list mailing list