PEP 255: Simple Generators

Jeff Shannon jeff at ccvcorp.com
Wed Jun 20 19:12:44 EDT 2001


David Eppstein <eppstein at ics.uci.edu> wrote in message news:<eppstein-86EFBD.11420820062001 at news.service.uci.edu>...
> I'm wondering if it wouldn't make sense to allow a non-empty return 
> statement in a generator, with the semantics that the object being returned 
> is itself some kind of iterator that is used to continue the sequence.
> 
> i.e.
>     def integers(i):
>         yield i
>         return integers(i+1)
> 
> ...thus allowing a more functional-programming-like lazy iteration.
> There would thus also be very little difference between a function that 
> returns an iterator, and a generator that does some yields before returning 
> an iterator.

It seems to me that this would be better written as

def integers(i):
    while 1:
        yield i
        i += 1

but I confess that I don't see offhand how either case generalizes to a 
less-contrived example.  It doesn't *seem* to me that your proposal adds
significant functionality...  but I'm not so familiar with functional 
programming, so I'm likely missing something.   :)

Jeff Shannon
Technician/Programmer
Credit International



More information about the Python-list mailing list