wrapping yield ?

Duncan Booth duncan at NOSPAMrcp.co.uk
Fri Sep 13 06:38:59 EDT 2002


"Michael Sparks" <Michael.Sparks at rd.bbc.co.uk> wrote in
news:alsdpt$2nq$1 at nntp0.reith.bbc.co.uk: 

> Whereas if I could wrap up yield, I simply have to do:
> 
> item = waitForSomethingCheckingPeriodically()
> process(item)
> 
> Which if I have lots of this sort of thing going on, results in much
> clearer (and hence maintainable) code.
> 
> If it can't (putting yield in a function, and not turning the function
> into a
> generator) be done, and AFAICT it can't, and this is a suggested work
> around - many thanks :-)

The syntax wouldn't look quite so bad if your function wasn't returning a 
value. At present 'for x in gen(): yield x' is the only way to nest 
generators, but there were discussions here recently suggesting a new 
syntactic sugar:
   yield every gen()
as an equivalent for the for loop.

There definitely isn't any way to put a yield in a function and not have it 
turn into a generator. When you yield from a generator, only a single frame 
is saved, not a stack, so you can only yield from the outer loop. By 
yielding at each level you effectively simulate the stack since each frame 
chains to the others.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list