• Python Global和Nonlocal的用法


    nonlocalglobal 也很容易混淆。简单记录下自己的理解。

    解释

    global

    总之一句话,作用域是全局的,就是会修改这个变量对应地址的值。

    global 语句是一个声明,它适用于整个当前代码块。 这意味着列出的标识符将被解释为全局变量。 尽管自由变量可能指的是全局变量而不被声明为全局变量。

    global语句中列出的名称不得用于该全局语句之前的文本代码块中。

    global语句中列出的名称不能定义为形式参数,也不能在 for 循环控制目标、class定义、函数定义、import语句或变量注释中定义。

    当前的实现并不强制执行这些限制,但是程序不应该滥用这种自由,因为未来的实现可能会强制执行这些限制,或者悄悄地改变程序的含义。

    程序员注意: global是指向解析器的指令。 它仅适用于与全局语句同时解析的代码。 特别是,包含在提供给内置 exec()函数的字符串或代码对象中的全局语句不会影响包含函数调用的代码块,而且这种字符串中包含的代码不会受包含函数调用的代码中的全局语句的影响。 eval()compile()函数也是如此。

    在这里插入图片描述

    nonlocal

    只在闭包里面生效,作用域就是闭包里面的,外函数和内函数都影响,但是闭包外面不影响。

    nonlocal 语句使列出的标识符引用除global变量外最近的封闭范围中的以前绑定的变量。 这很重要,因为绑定的默认行为是首先搜索本地名称空间。 该语句允许封装的代码将变量重新绑定到除全局(模块)作用域之外的本地作用域之外。

    nonlocal语句中列出的名称与global语句中列出的名称不同,它们必须引用封闭范围中已经存在的绑定(无法明确确定应在其中创建新绑定的范围)。

    举例

    没有用 nonlocalglobal

    x = 0
    def outer():
        x = 1
        def inner():
            x = 2
            print("inner:", x)
    
        inner()
        print("outer:", x)
    
    outer()
    print("global:", x)
    
    # inner: 2
    # outer: 1
    # global: 0
    

    nonlocal 的作用范围

    x = 0
    def outer():
        x = 1
        def inner():
            nonlocal x
            x = 2
            print("inner:", x)
    
        inner()
        print("outer:", x)
    
    outer()
    print("global:", x)
    
    # inner: 2
    # outer: 2
    # global: 0
    

    global 的作用范围

    x = 0
    def outer():
        x = 1
        def inner():
            global x
            x = 2
            print("inner:", x)
    
        inner()
        print("outer:", x)
    
    outer()
    print("global:", x)
    
    # inner: 2
    # outer: 1
    # global: 2
    

    注意

    • 本地的变量声明为global,就不能在再声明为nonlocal
    x = 0
    def outer():
        global x
        x = 1
        def inner():
            nonlocal x
            x = 2
            print("inner:", x)
        inner()
        print("outer:", x)
    outer()
    print("global:", x)
    
    # SyntaxError: no binding for nonlocal 'x' found
    
    

    • 使用nonlocal之前需要初始化变量
    x = 0
    def outer():
        def inner():
            nonlocal x
            x = 2
            print("inner:", x)
        inner()
        print("outer:", x)
    outer()
    print("global:", x)
    # SyntaxError: no binding for nonlocal 'x' found
    
    

    • 不能在函数的外部函数里面声明nonlocal
    x = 0
    def outer():
        x = 1
        nonlocal x
        def inner():
            x = 2
            print("inner:", x)
        inner()
        print("outer:", x)
    outer()
    print("global:", x)
    # SyntaxError: name 'x' is assigned to before nonlocal declaration
    
  • 相关阅读:
    生成R文件
    android开发问题汇总
    雅虎股票接口
    Ext4.1 , #Ext4.2
    MSSQL手工注入 报错注入方法
    MSSQL 数据库复制脚本
    Go VS Code 调式常见问题处理
    Win10 VS2012 无法注册IIS4.0 解决方案
    VirtualBox 局域网独立主机设置
    如何用.reg文件操作注册表
  • 原文地址:https://www.cnblogs.com/ievjai/p/14382666.html
Copyright © 2020-2023  润新知