[Python-Dev] Re: The iterator story

Ka-Ping Yee ping@zesty.ca
Sat, 20 Jul 2002 05:45:48 -0700 (PDT)


On Fri, 19 Jul 2002, Neil Schemenauer wrote:
> First, people could implement __iter__ such that it returns an iterator
> the mutates the original object (e.g. a file object __iter__ that
> returns xreadlines).

Yes, but then they would be violating the convention.  The way things
currently stand, we aren't even able to say what the convention *is*.

> Second, it will be confusing to have two different ways of looping over
> things.

It's a difference in perspective.  To me it seems confusing to have
only one way of looping that might do two different things.

But Guido basically agrees with you.  (As in, destructive and
non-destructive looping are not really that different; or, they are
different but it's not worth the bother.)

> Now I want to use this library but I have an iterator, not something
> that implements __iter__.  I would need to create a little wrapper with
> a __iter__ method that returns my object.

Yeah, that's seq().

> To summarize, I agree that "for" mutating the object can be surprising.

The rub is, the only way for it to *not* be surprising is to have a
way to *say* "loop destructively".  If you can't express your
expectations, there's no way to meet them.


-- ?!ng