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

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

    Python 3.5 or greater:

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

提交回复
热议问题