Why does this not work? How can I make it work? That is, how can I make gu accessible inside my decorated function?
def decorate(f):
def new_f():
def gu():
pass
f()
return new_f
@decorate
def fu():
gu()
fu()
Do I need to add gu to a dictionary of defined functions somehow? Or can I add gu to the local namespace of f before calling it?
In principle you can create a new function using the same code as the old one but substituting the global scope with an amended one:
In practice you really should find a better way to do this. Passing in functions as parameters is one option. There might be other approaches too, but it’s hard to tell what would fit without a high-level problem description.