PEP 255: Simple Generators

David Bolen db3l at fitlinxx.com
Wed Jun 20 20:52:32 EDT 2001


"Rainer Deyke" <root at rainerdeyke.com> writes:

> "David Bolen" <db3l at fitlinxx.com> wrote in message
> news:ur8wevrz0.fsf at ctwd0143.fitlinxx.com...
> > But isn't a generator really just a function that is returning an
> > iterator?  That is, why is it "very" different from a function,
> > internal implementation aside, which we agree it's not necessary to
> > reveal.
> 
> o = f()
> 
> To the caller, it doesn't matter if 'f' is a function, a generator, a bound
> method, a class, or any other callable object.  That does *not* mean that
> functions, generators, classes and other callable objects should have the
> same syntax: they are fundamentally different ways of implementing callable
> objects.

But neither does it necessarily mean they all need different syntax.
It seems that much of the disagreement here lies in whether someone
thinks that a function serving as a generator has "crossed the line"
beyond what a typical function (using the standard definition syntax)
ought to be able to do without special notation (beyond yield).  I
suppose that means there's no single answer, since I think that line
is far from black and white given all the possibilities for functions
today.

>           If generators absolutely have to reuse syntax (which is not the
> case), I would prefer the keyword 'class' over 'def', since calling a
> generator creates an instance.

But so does any number of factory functions that return objects (form
closures, etc...), e.g.:

    def f():
        return UserList.UserList([1,2,3])

(using UserList to avoid the type/class question)

So do we need different syntax for all types of functions that might
return instances of some object?  Eventually this could devolve into
the point elsewhere in the thread of wanting defint rather than def
for functions returning an integer.

>                                 Reusing 'class' also increases readability
> since it's easier to see the difference between a generator and a class by
> looking at the contents.

I guess I just don't see why this generator PEP needs such a change,
but clearly different people can differ on this :-)

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list