pythonbook/Python 基础教程/1.9.1 D函数 变量作用域.py

15 lines
613 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# global关键字声明的变量必须在全局作用域上不能嵌套作用域上
# 当要修改嵌套作用域enclosing作用域外层非全局作用域中的变量怎么办呢这时就需要nonlocal关键字了
def outer():
count = 10
def inner():
# nonlocal count
# global count
count = 100
print(count)
inner()
print(count)
outer()
# 当内部作用域想修改外部作用域的变量时就要用到global和nonlocal关键字了
# 当修改的变量是在全局作用域global作用域上的就要使用global先声明一下