mutable list iterators - a proposal

Tim Peters tim.one at comcast.net
Mon Mar 15 11:13:06 EST 2004


[Jess Austin]
>> I like the way that Python does lists, and I love the way it does
>> iterators.  But I've decided I don't like what it does with iterators
>> of lists.
>> ...

[Aahz]
> I'll guarantee that it won't be fixed for 2.4.  This subject has come
> up many times long before iterators were introduced, and the answer
> has always beent the same: if you want to mutate, make a copy or make
> *very* sure that your mutations don't muck the loop.

It's worse than just that:  the Python Reference manual defines what happens
when you mutate a sequence while iterating over it, even while discouraging
it.  So the precise behavior of, e.g.,

>>> r = range(10)
>>> for x in r:
...     print x
...     r.remove(x)
0
2
4
6
8
>>>

is actually advertised behavior.  See the Warning block at the end of
section "The 'for' statement".





More information about the Python-list mailing list