[root@test1 classes]# more 1.py
def scope_test():
    def do_local():
        spam = "local spam"
    def do_nonlocal():
        nonlocal spam
        spam = "nonlocal spam"
    def do_global():
        global spam
        spam = "global spam"
    spam = "test spam"
    do_local()
    print("After local assignment:", spam)
    do_nonlocal()
    print("After nonlocal assignment:", spam)
    do_global()
    print("After global assignment:", spam)
scope_test()
print("In global scope:", spam)
结果输出如下:
[root@test1 classes]# python 1.py
After local assignment: test spam
After nonlocal assignment: nonlocal spam
After global assignment: nonlocal spam
In global scope: global spam
[root@test1 classes]#


作用域范围:①do_global()的范围,作用域是在调用scope_test()外

           ②do_local()的范围,作用域只能在do_local()函数体内

           ③do_nonlocal()的范围,作用域在scope_test()函数体内,所以会覆盖scope_test函数定义的spam值。