Python vars() global name error

前端 未结 5 741
一整个雨季
一整个雨季 2021-01-21 08:31

I\'m having a bit of trouble understanding what\'s going wrong with the following function:

def ness():
 pie=\'yum\'
 vars()[pie]=4
 print vars()[pie]
 print yum         


        
5条回答
  •  情歌与酒
    2021-01-21 09:18

    vars() is equivalent to locals(), which in the case of the function is the local variables in its scope and at in the interactive interpreter at the scope you have it, vars() is globals(). locals() is for reading only; the effects of trying to change it are undefined (and in practice, just doesn't work). globals() can be modified, but you still should never directly put anything in the dict it returns.

提交回复
热议问题