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

前端 未结 6 1759
心在旅途
心在旅途 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:36

    globalVar = 25
    
    def myfunc(paramVar):
        localVar = 30
        all_vars = locals.copy()
        all_vars.update(globals())
        print "Vars: {globalVar}, {paramVar}, {localVar}!".format(all_vars)
    
    myfunc(123)
    

提交回复
热议问题