On Thu, 31 May 2018 10:05:43 +0200, Frank Millman wrote: > "Frank Millman" wrote in message news:pemchs$r12$1...@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.
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. py> d = dict(zip(range(5), "abcde")) py> for x in d: ... d[999] = 'mutation!' ... break ... py> d # no error occurred {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 999: 'mutation!'} To be more precise, the checks against mutation occur when next() is called. If you don't call next(), the checks don't run. py> it = iter(d) py> next(it) 0 py> del d[4] py> next(it) Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: dictionary changed size during iteration -- Steven D'Aprano "Ever since I learned about confirmation bias, I've been seeing it everywhere." -- Jon Ronson -- https://mail.python.org/mailman/listinfo/python-list