#!/usr/bin/python
# Filename: func_global.py
def func():
global x
print 'x is',x 在这显示global name 'x' is not defined
x = 2
print 'Changed local x to',x
x = 50
func()
print 'Value of x is',x
你需要最开始定义x
#!/usr/bin/python
# Filename: func_global.py
x = 0 #定义x
def func():
global x
print 'x is',x 在这显示global name 'x' is not defined
x = 2
print 'Changed local x to',x
x = 50
func()
print 'Value of x is',x