Skip to content Skip to sidebar Skip to footer

How Can I Dynamically Execute Function In Current Scope And Add It As Property Of The Calling Function?

I have some code like this: def f1(): . . . @mylib.codegen def f2(args): f1() mylib.py : def codegen(fn): src = inspe

Solution 1:

The fn.func_globals contains the global namespace for a given function; you'll need it to be able to exec the transformed and recompiled code object:

myscope = {}
myscope.update(fn.func_globals)

Do not use fn.func_globals directly; you wouldn't want to overwrite items in that namespace.

Post a Comment for "How Can I Dynamically Execute Function In Current Scope And Add It As Property Of The Calling Function?"