access to generator state

N

Neal D. Becker

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)
 
K

Kent Johnson

Neal said:
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)

You could make the generator a method of a class and store the generator state in instance variables
instead of local variables:
.... def __init__(self):
.... self.i = 0
.... def __call__(self):
.... while True:
.... yield self.i
.... self.i += 1
....1

HTH
Kent
 
S

Sean Ross

Neal D. Becker said:
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)
.... for i in range(0,2):
.... yield i
....Traceback (most recent call last):

HTH,
Sean
 
P

Peter Otten

Neal said:
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

You get read access with generator.gi_frame.f_locals and can mess (as
always) with mutable variables, but not rebind the local variables:
.... a = 2
.... b = [3]
.... yield a*b[0]
.... yield a*b[0]
....
g = gen()
g.gi_frame.f_locals {}
g.next() 6
g.gi_frame.f_locals {'a': 2, 'b': [3]}
g.gi_frame.f_locals["a"] = 137 # has no effect
g.gi_frame.f_locals["b"][0] = 42
g.next() 84
g.gi_frame.f_locals
{'a': 2, 'b': [42]}

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,212
Messages
2,571,102
Members
47,698
Latest member
TerraT521

Latest Threads

Top