question about generators
imbosol at vt.edu
Wed Aug 14 23:16:56 CEST 2002
Jonathan Hogg wrote:
> On 14/8/2002 16:38, in article yu993cthtpqr.fsf at europa.research.att.com,
> "Andrew Koenig" <ark at research.att.com> wrote:
>> I had a function along the following lines:
>> def f():
>> if <condition>:
>> print <something>
>> <do something>
>> <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>'
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