Can I define a function which, when called, inserts new locals into the caller\'s scope? I have a feeling that passing the caller\'s locals() into the function migh
This is not possible without changing the API, as CPython retains a reference to the argument list for the duration of the function call.
One way to do this is to pass an object, and clear the object's internal state when done using it.
class Obj(object):
def __init__(self, state):
self.state = state
def f(o):
# Do stuff with o.state
del o.state