generator object or 'send' method?
Terry Reedy
tjreedy at udel.edu
Mon Feb 9 14:22:52 EST 2009
Aaron Brady wrote:
> Hello,
>
> I am writing a generator to return a sequence of numbers with some
> variation. The parameters of the variation can be changed by the
> caller, even after the generator is started. My question is, is it
> better to wrap the generator in an object, so that the parameters can
> be changed just by an attribute access?
Rather than wrap a generator, I would just write an iterator class.
A generator function is basically an abbreviated class statement. (The
abbreviation is the removal of boilerplate code.)
> Or keep the generator clear, and modify parameters with a 'send' call?
If I were modifying a single parameter and wanted the next value
returned immediately upon sending, I would do this.
> P.S. It would receive the 'send' from a different point in control
> flow than its usual 'next'. Should it repeat a value if it receives a
> 'send'? Or should I wrap it in a secondary 'trap_send_and_repeat'
> generator?
But for this I would go with the class. Pretty foolproof.
tjr
More information about the Python-list
mailing list