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
posted @ 2018-05-26 09:35  nbumaster  阅读(538)  评论(0编辑  收藏  举报