Problem with OrderedDict - progress report
Frank Millman
frank at chagford.com
Thu May 31 04:05:43 EDT 2018
"Frank Millman" wrote in message news:pemchs$r12$1 at blaine.gmane.org...
>
> So working backwards, I have solved the first problem. I am no nearer to
figuring out why it fails intermittently in my live program. The message
from INADA Naoki suggests that it could be inherent in CPython, but I am not
ready to accept that as an answer yet. I will keep plugging away and report
back with any findings.
>
Ok, I have not found the root cause yet, but I have moved the problem to a
different place, which is progress.
>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. Adding a key while processing any prior key in an OrderedDict
does raise the exception. I have checked this fairly thoroughly and it
behaves the same way every time.
>>> from collections import OrderedDict as OD
>>> d = OD()
>>> d[1] = 'one'
>>> d[2] = 'two'
>>> for k in d:
... if k == 2:
... d[3] = 'three'
...
>>> d = OD()
>>> d[1] = 'one'
>>> d[2] = 'two'
>>> for k in d:
... if k == 1:
... d[3] = 'three'
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: OrderedDict mutated during iteration
>>>
The intermittent nature of my problem stems from the above - sometimes I add
a key while processing the last key, sometimes a prior one. I don't know why
this is happening, so I am still investigating, but it has moved into the
realm of normal debugging, not chasing shadows.
Frank
More information about the Python-list
mailing list