[Python-Dev] Single- vs. Multi-pass iterability

Barry A. Warsaw barry@zope.com
Fri, 19 Jul 2002 10:59:33 -0400


>>>>> "KY" == Ka-Ping Yee <ping@zesty.ca> writes:

    KY> It's just not the way i expect for-loops to work.  Perhaps we
    KY> would need to survey people for objective data, but i feel
    KY> that most people would be surprised if

    |     for x in y: print x
    |     for x in y: print x

    KY> did not print the same thing twice, or if

As with many things Pythonic, it all depends.  Specifically, I think
it depends on the type of y.  Certainly in a pre-iterator world there
was little preventing (or encouraging?) you to write y's __getitem__()
non-destructively, so I don't see much difference if y is an iterator.

    KY> Even if it's okay for for-loops to destroy their arguments, i
    KY> still think it sets up a bad situation: we may end up with
    KY> functions manipulating sequence-like things all over, but it
    KY> becomes unclear whether they destroy their arguments or not.
    KY> It becomes possible to write a function which sometimes
    KY> destroys its argument and sometimes doesn't.  Bugs get deeper
    KY> and harder to find.

How is that different than pre-iterators with __getitem__()?

    KY> I'm assigning properties to "for" that you aren't.  I think
    KY> they are useful properties, though, and worth considering.

These aren't properties of for-loops, they are properties of the
things you're iterating (little-i) over.

-Barry