On Sat, Jun 2, 2018 at 9:10 AM, Gregory Ewing <greg.ew...@canterbury.ac.nz> wrote: > Chris Angelico wrote: >> >> if you 'break' immediately after a mutation, that isn't >> continuing to iterate. Even though you're inside the loop, there's no >> further action taken to process the loop, and no problem. > > > Yes, but you're also not calling next() again, so no > exception would be triggered. > > My point is that I don't see a justification for treating > the last next() call (the one that notices you've reached > the end and raises StopIteration) any differently from > the others. It seems like either a bug or a misfeature > to me. >
I'm guessing it's pure chance, and that certain types of mutation would still trigger the exception. A bit of background: These exceptions exist to prevent interpreter crashes. I believe originally you'd only get an exception if the mutation triggered a dictionary resize; in other words, if NOT giving an exception would have actually segfaulted the interpreter right then and there. (I'm talking CPython here, obviously. No idea what other Pythons did at the time.) It's more consistent now; but it looks like you may have come across a residual inconsistency. ISTM it would indeed be more logical to raise for this situation too. ChrisA -- https://mail.python.org/mailman/listinfo/python-list