Closed chenhsi closed 10 years ago
As a reference, Python just iterates using the index:
l = [0, 1, 2, 3]
for i in l:
if i == 1:
l.insert(2, 1000)
print i
Will print 0, 1, 1000, 2, 3 (on separate, lines, of course).
That seems fine to me. The only other option I'm seeing right now is making a copy of the list and iterating over that (ensuring that the user never modifies what is being iterated over), but that sounds like it would be pretty inefficient and unintuitive.
Seems fine to me. Since this has been discussed and agreed on, and is also the current behavior, I'll just close this issue then.
What should be the desired behavior if an iterable object is modified (i.e. item added/removed/changed) in the middle of an iteration? As the only support for iterations present is the for loop, this issue is directly related to that. While the current code does not error or have unpredictable behavior in such an occurrence, I'm uncertain if its behavior (i.e. iterate based on index and ignore any values) is necessarily the desired one.
And while I am quite tempted to simply condemn the user for doing such a practice, the lack of a traditional C/Java-style for loop that would allow the user to more carefully specify behavior (which is my usual work-around) worries me (although I suppose that a while loop could always suffice).