<jupyter_start><jupyter_text>Python 变量可见性参考： [Python Notes: Variable Visibility](https://baihuqian.github.io/2018-09-08-python-variable-visibility/:~:text=Without%20nonlocal%20keyword%2C%20Python%20creates%20a%20new%20variable,declaring%20a%20new%20variable%20that%20shadows%20its%20name.)  变量的全局可见性Python 的变量是全局可见的：- 函数内部可以访问外部变量；- 函数内部不能对变量进行修改；- 函数内的同名变量为局部变量。<jupyter_code>msg = 'Hello World!'

def fun1():
    print(msg)
    

fun1()
def fun2():
    # 只能访问不能修改，将会报错
    msg = msg + ' Hello Python!'
    

fun2()
def fun3():
    msg = 'Hello Python!'
    print(msg)
    
    
print(msg)
fun3()<jupyter_output>Hello World!
Hello Python!<jupyter_text>全局变量与非局部变量- 全局变量使用 `global` 关键字声明，所有函数均可以访问和修改；- 非局部变量使用 `nonlocal` 关键字声明，仅与上一级函数共享。<jupyter_code>def fun41():
    global global_msg
    global_msg = 'Good Afternoon!'
    
    def fun42():
        global global_msg
        global_msg = 'Good Evening!'
        
        def fun43():
            global global_msg
            global_msg = 'Good Night!'
            print(global_msg)
        
        
        fun43()
        # fun43被调用后所有 global_msg 重新幅值为 'Good Night!'
        print(global_msg)
    
    fun42()
    # 已经被 fun43 重载
    print(global_msg)
    

global_msg = 'Good Morning!'
print(global_msg)
fun41()
print(global_msg)
def fun41():
    nonlocal_msg = 'Good Afternoon!'
    
    def fun42():
        nonlocal_msg = 'Good Evening!'
        
        def fun43():
            nonlocal nonlocal_msg
            nonlocal_msg = 'Good Night!'
        
        
        fun43()
        # 最内层程序调用，并且通过 nonlocal 将变量传递到上一层
        # fun42 内部变量被 fun43 重新赋值
        print(nonlocal_msg)
    
    fun42()
    # fun42 内部为局部变量，不做传递
    print(nonlocal_msg)
    

nonlocal_msg = 'Good Morning!'
print(nonlocal_msg)
fun41()
print(nonlocal_msg)<jupyter_output>Good Morning!
Good Night!
Good Afternoon!
Good Morning!
{'fraction_non_alphanumeric': 0.10866057019903173, 'fraction_numerical': 0.04142011834319527, 'mean_word_length': 2.795918367346939, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '559088', 'license': 'MIT', 'n_tokens_mistral': 826, 'n_tokens_neox': 825, 'n_words': 124, 'path': 'notes/.ipynb_checkpoints/variable_visibility-checkpoint.ipynb', 'repo_name': 'iChunyu/LearnPython', 'size': '1859'}