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

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

    You could make your own:

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

    or combine the first two lines:

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

提交回复
热议问题