Python在函数中使用全局变量的问题
时间:2014-02-09 15:56:36
收藏:0
阅读:447
在函数中定义的局部变量如果和全局变量同名,则它会隐藏该全局变量。如果想在函数中使用全局变量,则需要使用global进行声明。举例说明如下:
x = 1
def func():
x = 2
print x
print x
func()
print x
执行结果为:
1 2 1
x = 1
def func():
global x
print x
x = 2
print x
print x
func()
print x执行结果为:
1 1 2 2
原文:http://blog.csdn.net/warren912/article/details/18988757
评论(0)