# num1 is a global variable
num1 = 1
print num1
# num2 is a local variable
def fun():
num1 = 2
num2 = num1 + 1
print num2
fun()
# the scope of global num1 is the whole program, num 1 remains defined
print num1
# the scope of the variable num2 is fun(), num2 is now undefined
print num2
结果:
1 3 1NameError: name 'num2' is not defined
example2:
num = 4
def fun1():
global num
num = 5
def fun2():
global num
num = 6
# note that num changes after each c