mutable list iterators - a proposal

Jess Austin jaustin at post.harvard.edu
Mon Mar 22 18:40:38 EST 2004


I hope I'm not belaboring the point.  b-)

python at rcn.com (Raymond Hettinger) wrote in message news:<5d83790c.0403211210.703991c2 at posting.google.com>...
> When designing a new type, the trick is to avoid areas where two
> different people could reasonably expect different "normal" behaviors.
>  Otherwise, you're doing more harm than good and making it likely that
> your users will stumble into a class of bugs that are extremely
> difficult to hunt down.

This seems like good advice, but the current implementation of lists
in Python doesn't follow it wrt modifying while iterating.  People who
remember all the footnotes to the docs expect one thing, and the rest
of us expect something else.  Maybe it would be better if list
iterators puked in the same fashion that dict iterators do immediately
after the dict instance is modified.  Then users wouldn't start by
appending to lists over which they are iterating (which works,
although I'm not sure it will with the new reversed iterators), and go
on to expect to be able to modify lists in a general fashion.


> The code is an improvement, but still suffers from the definitional
> ambiguity mentioned above.  I believe this is inescapable.  Consider a

Thanks.  I feel that in allowing an operation that was previously
implicitly forbidden, one can be forgiven if there is some subtlety to
the semantics of the operation.  But that's debatable.


> All the contortions in the code still suggest to me that the use case
> needs a different algorithm.  Try rewriting it with separate input and
> output lists.  My bet is that the code is clearer and more
> maintainable.

My original use case was 3 classes in 30 lines, not including the
subclassing of list.  I thought the subclassing of list was fairly
componentized, and it would be unnecessary if the list implementation
were modified as I've suggested.  That doesn't look too likely. b-) 
I'll certainly be using collections.deque after I install 2.4!


> > import itertools
> 
> Hey, hey, I love to see people using my module ;-)

I love to use it.  Iterators and list comprehensions were good, but
coupled with itertools they are great.

Thanks, Raymond, for all you've done for Python.

later,
Jess



More information about the Python-list mailing list