#!/usr/bin/python
#coding:utf-8
def printNum():
print "num : %d" % num#可以读取全局变量
num = 10
if __name__ == "__main__":
print "num is: %d" % num
printNum()
else:
print "not in main..." #!/usr/bin/python
#coding:utf-8
def printNum():
num += 5#给全局变量赋值错误
print "num : %d" % num
num = 10
if __name__ == "__main__":
print "num is: %d" % num
printNum()
else:
print "not in main..."
#!/usr/bin/python
#coding:utf-8
def printNum():
global num#如果需要给全局变量赋值,加上global
num += 5
print "num : %d" % num
num = 10
if __name__ == "__main__":
print "num is: %d" % num
printNum()
else:
print "not in main..."
#!/usr/bin/python
#coding:utf-8
def printNum():
global num
num += 5
print "num : %d" % num
def printArray():
ary.append("hello")#可以改变全局变量
global ary#如果直接赋值的话会出错,所以需要加上global
ary = ["new array"]
print ary
num = 10
ary = ["one"]
if __name__ == "__main__":
print "num is: %d" % num
print "array is :",ary
printNum()
printArray()
else:
print "not in main..."原文:http://blog.csdn.net/iitvip/article/details/19974439