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

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

    Interpolation into strings works in the simplest possible way. Just list your variables. Python checks locals and globals for you.

    globalVar = 25
    
    def myfunc(paramVar):
        localVar = 30
        print "Vars: %d, %d, %d!" % ( globalVar, paramVar, localVar )
    
    myfunc(123)
    

提交回复
热议问题