Python全局变量 [英] Python global variable
本文介绍了Python全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
def say_boo_twice():
global boo
boo = 'Boo!'
print boo, boo
boo = 'boo boo'
say_boo_twice()
输出为
嘘!嘘!
与我预期的不同.由于我将boo
声明为全局变量,所以输出为何不这样:
Not as I expected. Since I declared boo
as global, why is the output not:
嘘嘘嘘嘘
推荐答案
您已在函数内更改了boo
,为什么不更改?而且,全局变量是错误的.
You've changed boo
inside your function, why wouldn't it change? Also, global variables are bad.
这篇关于Python全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文