x = 0 y = 0 def incr(x): y = x + 1 return y incr(5)print x, y
全局变量,x,y不会影响到函数中的变量,但是我们可以在函数中使用全局变量
pi = 3.14 def area(r): return pi * r * r使用global
numcalls = 0 def square(x): global numcalls numcalls = numcalls + 1 return x * x
x = 0 y = 0 def incr(x): y = x + 1 return y incr(5)print x, y
全局变量,x,y不会影响到函数中的变量,但是我们可以在函数中使用全局变量
pi = 3.14 def area(r): return pi * r * r使用global
numcalls = 0 def square(x): global numcalls numcalls = numcalls + 1 return x * x