How can I modify the local namespace of a function in python? I know that locals() returns the local namespace of the function when called inside it, but I want to do somet
Since the function isn't invoked, it has no "local" stack frame, yet. The most simple solution is to use a global context:
handler = None
def f():
handler()
def g(): pass
handler = g
Or you could set g on the function object:
f.g = g
But I'm not sure how you can get the function object from within the function itself. If it was a method, you would use self.