Global and local变量的区别

脚本一:

str = '1'
print ('%15s = %s' %('original',str))
def test(): 
     str = '0'
     print ('%15s = %s' %('in function',str))
test()

执行结果:

        original = 1
     in function = 0
    out function = 1

脚本二:

str = '1'
print ('%15s = %s' %('original',str))
def test(): 
    global str    

    str = '0'
     print ('%15s = %s' %('in function',str))
test()

执行结果:

       original = 1
     in function = 0
    out function = 0

猜你喜欢

转载自www.cnblogs.com/netbackup/p/9091865.html