[Python-ideas] Mutating while iterating

Aaron Brady castironpi at gmail.com
Sun Aug 3 19:46:08 CEST 2014


On Sat, Jul 26, 2014 at 10:39 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> On 27 July 2014 06:51, Aaron Brady <castironpi at gmail.com> wrote:
>> Hi, I asked about the inconsistency of the "RuntimeError" being raised when
>> mutating a container while iterating over it here [1], "set and dict
>> iteration" on Aug 16, 2012.
>
> Hi,
>
> This is clearly an issue of grave concern to you, but as Raymond
> pointed out previously, you appear to have misunderstood the purpose
> of those exceptions. They're there to prevent catastrophic failure of
> the interpreter itself (i.e. segmentation faults), not to help find
> bugs in user code. If users want to mutate containers while they're
> iterating over them, they're generally free to do so. The only time
> we'll actively disallow it is when such mutation will outright *break*
> the iterator, rather than merely producing potentially surprising
> results.
>
> I have closed the new issue and added a longer reply (with examples)
> that will hopefully better explain why we have no intention of
> changing this behaviour: http://bugs.python.org/issue22084#msg224100


Python is replete with examples of prohibiting structures which are
likely bugs but aren't segfaults.  There are also reciprocal-- though
not necessarily inverse-- limitations of the unordered collections
themselves ("set" and "dict").  The new behavior is transparent for
the programmer; no possible programs can /rely/ on the existing
behavior.  The new behavior introduces no new objects, possibly except
"IterationError", no new syntax, and no new costs.

I propose we leave this discussion thread open for the time being.  I
also take the issue of /re/-assigning to keys during iteration to be
settled as permitted.


More information about the Python-ideas mailing list