def a():
# global x
# print "*****x is:%d"%x
x=10
print "$$$$$$$x is:%d"%x
x=50
a()
print "&&&&&&&x is :%d"%x
btc@ubuntu:~/Downloads$ python g.py
$$$$$$$x is:10
&&&&&&&x is :50
def a():
# global x
print "*****x is:%d"%x
x=10
print "$$$$$$$x is:%d"%x
x=50
a()
print "&&&&&&&x is :%d"%x
Traceback (most recent call last):
File "g.py", line 7, in
a()
File "g.py", line 3, in a
print "*****x is:%d"%x
UnboundLocalError: local variable 'x' referenced before assignment
def a():
global x
print "*****x is:%d"%x
x=10
print "$$$$$$$x is:%d"%x
x=50
a()
print "&&&&&&&x is :%d"%x
btc@ubuntu:~/Downloads$ python g.py
*****x is:50
$$$$$$$x is:10
&&&&&&&x is :10
函数中引用的变量默认的是局部变量 局部变量可以跟全局变量名字相同 如果在局部变量前边加global 就自动引用全局的
阅读(1255) | 评论(0) | 转发(0) |