globals () is not suitable, because it gives a dictionary of variable module where the function was initialized, which does not always coincide with the call site. Of course you can explicitly transmit VARS () to the function, but is there no way it is more elegant?
Answer # 1
"" @file funcs.py "" " Import sys DEF FUNCTION (): Try: depth= 1. Frame= sys._getFrame (Depth) While Frame.f_code.co_name!= "< Module > ": Frame= sys._getFrame (Depth) depth += 1 Print (Frame.f_globals) Except Valueerror: RAISE EXCEPTION ("It did not work out to determine the caller module.")
"" @file __init__.py "" " from FUNCS Import Function DEF FUNCTION_W (REC= 50): # Increases the call stack in function (). IF REC > 0: FUNCTION_W (REC -1) ELSE: FUNCTION () Hello= "World" Function_W ()
Frame.'s from a call stack until we find the module (we identify it by name
≪ Module >). After finding us available to its global variables through the attribute