PEP 255: Simple Generators, Revised Posting

Bernhard Herzog bh at intevation.de
Mon Jun 25 04:57:25 EDT 2001


Neil Schemenauer <nas at python.ca> writes:

> Bernhard Herzog wrote:
> > It seems to me that as it stands, empty generators are a bit too
> > difficult to write, so I think we need an easier way to write them. One
> > way to write them would be
> > 
> >     def g():
> >         yield
> 
> 
> What is wrong with:
> 
>     def g():
>         return []

If I have another function f that expects g to return an iterator this
wouldn't work.

> 
> If you really want an iterator object you can do:
> 
>     def g():
>         return iter([])

Although this would, of course.

> I don't know why anyone would care though.

The only reason would be consistency, I guess, which in practice is not
necessarily important.
 
> > If generators weren't created with a def statement it would be a bit
> > more obvious:
> > 
> >    generator g():
> >         pass
> 
> I don't think Guido will be convinced that we need a new keyword just
> because it makes it easy to write silly code.

I wouldn't say it's silly. Just as a normal function that does nothing
is not silly either.

With regard to the generator keyword, I actually think that using def
for generators is correct, because even if the function contains yield,
the object created by the def statement is a function object. An of
course, a generator is pretty similar to a function.

  Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                               http://mapit.de/



More information about the Python-list mailing list