Get a dict of all variables currently in scope and their values

前端 未结 6 1752
心在旅途
心在旅途 2020-12-02 16:51

Consider this snippet:

globalVar = 25

def myfunc(paramVar):
    localVar = 30
    print \"Vars: {globalVar}, {paramVar}, {localVar}!\".format(**VARS_IN_SCOP         


        
6条回答
  •  自闭症患者
    2020-12-02 17:31

    Does this do what you intended?

    d = dict(globals())
    d.update(locals())
    

    If I read the documentation correctly, you create a copy of the globals() dict, then you overwrite any duplicates and insert new entries from the locals() dict (since the locals() should have preference within your scope, anyway).


    I haven't had any luck in getting a proper function to return the full dictionary of variables in scope of the calling function. Here's the code (I only used pprint to format the output nicely for SO):

    from pprint import *
    
    def allvars_bad():
        fake_temp_var = 1
        d = dict(globals())
        d.update(locals())
        return d
    
    def foo_bad():
        x = 5
        return allvars_bad()
    
    def foo_good():
        x = 5
        fake_temp_var = "good"
        d = dict(globals())
        d.update(locals())
        return d
    
    pprint (foo_bad(), width=50)
    pprint (foo_good(), width=50)
    

    and the output:

     {'PrettyPrinter': ,
     '__builtins__': ,
     '__doc__': None,
     '__file__': 'temp.py',
     '__name__': '__main__',
     '__package__': None,
     'allvars_bad': ,
     'd': ,
     'fake_temp_var': 1,
     'foo_bad': ,
     'foo_good': ,
     'isreadable': ,
     'isrecursive': ,
     'pformat': ,
     'pprint': ,
     'saferepr': }
    {'PrettyPrinter': ,
     '__builtins__': ,
     '__doc__': None,
     '__file__': 'temp.py',
     '__name__': '__main__',
     '__package__': None,
     'allvars_bad': ,
     'd': ,
     'fake_temp_var': 'good',
     'foo_bad': ,
     'foo_good': ,
     'isreadable': ,
     'isrecursive': ,
     'pformat': ,
     'pprint': ,
     'saferepr': ,
     'x': 5}
    

    Note that in the second output, we have overwritten fake_temp_var, and x is present; the first output only included the local vars within the scope of allvars_bad.

    So if you want to access the full variable scope, you cannot put locals() inside another function.


    I had suspected there was some sort of frame object, I just didn't (know where to) look for it.

    This works to your spec, I believe:

    def allvars_good(offset=0):
        frame = sys._getframe(1+offset)
        d = frame.f_globals
        d.update(frame.f_locals)
        return d
    
    
    def foo_good2():
        a = 1
        b = 2
        return allvars_good()
    

    -->

    {'PrettyPrinter': ,
     '__builtins__': ,
     '__doc__': None,
     '__file__': 'temp.py',
     '__name__': '__main__',
     '__package__': None,
     'a': 1,
     'allvars_bad': ,
     'allvars_good': ,
     'b': 2,
     'foo_bad': ,
     'foo_good': ,
     'foo_good2': ,
     'isreadable': ,
     'isrecursive': ,
     'pformat': ,
     'pprint': ,
     'saferepr': ,
     'sys': }
    

提交回复
热议问题