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

Ka-Ping Yee ping@zesty.ca
Wed, 17 Jul 2002 15:53:42 -0700 (PDT)


I wrote:
> __iter__ is a red herring.
[...blah blah blah...]
> The short of it is that whenever any Python programmer says
> "for x in y", he or she had better be darned sure of whether
> this is going to destroy y.  Whatever we can do to make this
> clear would be a good idea.

Guido wrote:
> This is a very good summary of the two iterator protocols.  Ping,
> would you mind adding this to PEP 234?

And i thought it was a critique.  Fascinating, Captain. :)

I'm happy to add the text, but i want to be clear, then: is it
acceptable to write an iterator that only provides <next> if you
only care about the "iteration protocol" and not the "for-able
protocol"?

I see that "ought to" is the most opinion the PEP is willing to
give on the topic:

    A class is a valid iterator object when it defines a next()
    method that behaves as described above.  A class that wants
    to be an iterator also ought to implement __iter__()
    returning itself.


-- ?!ng