[Python-iterators] Re: PEP 234: Iterators

Just van Rossum just at letterror.com
Tue May 1 05:14:59 EDT 2001

Christian Tanzer wrote:

> >     - Using the same name for two different operations (getting an
> >       iterator from an object and making an iterator for a function
> >       with an sentinel value) is somewhat ugly.  I haven't seen a
> >       better name for the second operation though.
> I assume `ugly' refers to the implementation.
> As both operations return an iterator object it seems natural to use
> the same name for them. Having to remember two different names for
> iterator-returning operations looks ugly to me.

But the usage is typically different. iter(obj) will _usually_ be used from
within an __iter__ method, which will typically be invoked implicitly by the for
loop. I think it will be rare for iterators created "manually" with iter(obj) to
be passed "manually" to a for loop.

On the other hand, the output of iter(callable, sentinel) *is* likely to be
passed "manually" to a for loop.

Conceptually, iter(obj) returns an iterator object, and iter(callable, sentinel)
returns an object that can be iterated over. However, due to the protocol's
requirement that iter(obj) is equivalent to iter(iter(obj)) this duality is not
visible, and possibly therefore not that important. However, it *is* IMHO worthy
of the qualification "somewhat ugly", but not much more than that ;-)


More information about the Python-list mailing list