Home>

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?

Well, in fact, in good quality variables, it is clearly transmitted to a class or function, everything else is some left khaki, so incorrect to write programs.

CrazyElf2021-04-07 17:32:02
  • 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 ()
    

    in Function. through SYS._GETFRAME.Rail 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 F_Globals. .

Trends