question about generators

Daniel Dittmar daniel.dittmar at sap.com
Wed Aug 14 18:16:52 CEST 2002


Andrew Koenig wrote:
>         def f():
>                 if <condition>:
>                         yield <something>
>                 else:
>                         <do something>
>                         for i in f():
>                                 yield i
>                         <do something else>
>
> So... my question is this:  Is there a cleaner general way of making
> this kind of program transformation?

The Unix way would be to use pipes/queues:
def f(queue):
    if <condition>:
        queue.append (...)
    else:
        <do something>
        f (queue)
        <do something else>

This of course requires a second thread for the queue to be read.
Maybe someone creates a
yield >>queue, value
to allow this in one thread.

Daniel






More information about the Python-list mailing list