question about generators

Bjorn Pettersen BPettersen at NAREX.com
Wed Aug 14 20:45:28 CEST 2002


> From: Andrew Koenig [mailto:ark at research.att.com] 

[snip turning a function into a generator]
                 
> So... my question is this:  Is there a cleaner general way of 
> making this kind of program transformation? Obviously, replacing
> 
>         for i in f():
>                 yield i
> 
> by
> 
>         yield f()
> 
> won't work -- nor should it, because the compiler doesn't 
> know that I might not be intending to write a generator that 
> yields a sequence of genrators.

I've noticed a similar problem, ie. all my generators are recursive and
I have to do the for-loop dance, and yes it had me flumoxed the first
time too.

It seems like this is a common enough idiom that it could warrant
special syntax(?) Coming from a C++ background (but having also
programmed in Python for the last five years) something like

  yield << f()

would seem natural, and also symetric to print >> ;->

-- bjorn




More information about the Python-list mailing list