Generator Frustration

Terry Reedy tjreedy at udel.edu
Mon Jun 20 21:42:40 EDT 2011


On 6/20/2011 6:04 PM, Joel wrote:
> On Jun 4, 2:27 pm, "TommyVee"<xxxxx... at xxxxxx.xxx>  wrote:
>> I'm using the SimPy package to run simulations. Anyone who's used this
>> package knows that the way it simulates process concurrency is through the
>> clever use of yield statements. Some of the code in my programs is very
>> complex and contains several repeating sequences of yield statements.  I
>> want to combine these sequences into common functions.  The problem of
>> course, is that once a yield gets put into a function, the function is now a
>> generator and its behavior changes.
...

> I actually found a reasonable answer to this, I think.  If one of the
> called functions contains a yield, that function is by definition a
> generator,

A nomenclature note: a function with yield is a 'generator function'. It 
is an instance of the 'function' class, same as for any other def 
statement (or lambda expression). It returns an instance of class 
'generator, as you note here

 > and will test as such with 'if type(result)==types.GeneratorType:'.

It is the result, and not the function, that is the generator (a type of 
iterator).

-- 
Terry Jan Reedy




More information about the Python-list mailing list