access to generator state

Neal D. Becker ndbecker2 at verizon.net
Thu Dec 2 13:36:34 EST 2004


I am converting optimization code from legacy C to python.  Generators are a
HUGE convenience, because the original code structures have the optimizer
as the main code calling your function, while I want to invert these roles. 
I want to call the optimizer to perform just one step at a time.

So, the optimizer must preserve it's state.  The classic way to do this is
with classes.  Problem is, I need to figure out just what variables need to
be preserved across calls.

Using yield, this is trivial to achieve.  All state is automatically saved.

Only one problem.  Is there any way to access the state of a generator
externally?  In other words, the generator saves all it's local variables. 
Can an unrelated object then query the values of those variables?  (In this
case, to get at intermediate results)




More information about the Python-list mailing list