原文:http://www.kuqin.com/abyteofpython_cn/ch07s03.html
#!/usr/bin/python
# Filename: func_global.py
def func():
global x
print 'x is', x
x = 2
print 'Changed local x to', x
x = 50
func()
print 'Value of x is', x输出
$ python func_global.py
x is 50
Changed global x to 2
Value of x is 2
本文通过一个简单的Python脚本示例介绍了全局变量的概念及其使用方法。脚本定义了一个名为func的函数,该函数内部声明了一个全局变量x,并展示了如何在函数内外改变并访问这个全局变量。
1万+

被折叠的 条评论
为什么被折叠?



