question about generators

Carl Banks imbosol at
Wed Aug 14 23:16:56 CEST 2002

Jonathan Hogg wrote:
> On 14/8/2002 16:38, in article yu993cthtpqr.fsf at,
> "Andrew Koenig" <ark at> wrote:
>> I had a function along the following lines:
>>       def f():
>>               if <condition>:
>>                       print <something>
>>               else:
>>                       <do something>
>>                       f()
>>                       <do something else>
>> and I wanted to turn it into a generator instead of having it
>> generate output directly.  My first try was to change "print"
>> to "yield", and that failed horribly.
> I'm confused, the pattern you show above will only ever 'print <something>'
> once.

Not so fast!  He could have two calls to f(), or the f() inside of a
loop.  Omitted for simplicity, of course.


More information about the Python-list mailing list