[Python-iterators] Re: PEP 234: Iterators

Christian Tanzer tanzer at swing.co.at
Wed May 2 01:49:58 EDT 2001

Just van Rossum <just at letterror.com> wrote:

> 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.

The typical use of `iter(seq)' will certainly be implicit. OTOH, I'd
expect the number of explicit uses of `iter(seq)' to be comparable to
the number of explicit uses of `iter(callable, sentinel)'.

> Conceptually, iter(obj) returns an iterator object, and iter(callable,
> sentinel) returns an object that can be iterated over.

That sentence is a refined example of hair splitting <wink>. The PEP
says that both return an iterator object. The fact that the iterator
object is constructed differently doesn't change the semantics of the
returned object.

> However, it *is* IMHO worthy of the qualification "somewhat ugly", but
> not much more than that ;-)

I suffered languages which forced me to use different names for the
same operation applied to different data types (e.g., sort_int vs.
sort_float vs. sort_spam...) for too long. I very much appreciate
Ada's and C++'s concept of overloading. There are obvious reasons why
in general overloading doesn't work in Python.

So IMO the only plausible complaint about the overloading of `iter' is
that a Python user cannot do the same thing (the implementation of the
overloading probably earns the predicate `ugly', too, but that isn't
relevant for Python's users). I wouldn't go back to C++ to get
overloading back, though :-)

Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92

More information about the Python-list mailing list