在Python中,什么是全局语句?

问题描述:

什么是全局声明?以及如何使用?我已阅读 Python的官方定义;
但是,这对我来说没有多大意义.

What is a global statement? And how is it used? I have read Python's official definition;
however, it doesn't make a lot of sense to me.

python中的每个变量"都限制在一定范围内. python文件"的范围是模块范围.请考虑以下内容:

Every "variable" in python is limited to a certain scope. The scope of a python "file" is the module-scope. Consider the following:

#file test.py
myvariable = 5  # myvariable has module-level scope

def func():
    x = 3       # x has "local" or function level scope.

具有局部作用域的对象会在函数退出后立即死亡,并且永远无法检索(除非您return),但是在函数内,您可以访问模块级作用域(或任何包含的作用域)中的变量:

Objects with local scope die as soon as the function exits and can never be retrieved (unless you return them), but within a function, you can access variables in the module level scope (or any containing scope):

myvariable = 5
def func():
    print(myvariable)  # prints 5

def func2():
    x = 3
    def func3():
        print(x)       # will print 3 because it picks it up from `func2`'s scope

    func3()

但是,您不能在该引用上使用赋值,并且期望它会传播到外部作用域:

However, you can't use assignment on that reference and expect that it will be propagated to an outer scope:

myvariable = 5
def func():
    myvariable = 6     # creates a new "local" variable.  
                       # Doesn't affect the global version
    print(myvariable)  # prints 6

func()
print(myvariable)      # prints 5

现在,我们终于到了global. global关键字是告诉python函数的特定变量是在全局(模块级)范围内定义的方式.

Now, we're finally to global. The global keyword is the way that you tell python that a particular variable in your function is defined at the global (module-level) scope.

myvariable = 5
def func():
    global myvariable
    myvariable = 6    # changes `myvariable` at the global scope
    print(myvariable) # prints 6

func()
print(myvariable)  # prints 6 now because we were able 
                   # to modify the reference in the function

换句话说,如果使用global关键字,则可以在func范围内在模块范围内更改myvariable的值.

In other words, you can change the value of myvariable in the module-scope from within func if you use the global keyword.

顺便说一句,合并范围可以任意深度嵌套:

As an aside, scopes can be nested arbitrarily deep:

def func1():
    x = 3
    def func2():
        print("x=",x,"func2")
        y = 4
        def func3():
            nonlocal x  # try it with nonlocal commented out as well.  See the difference.
            print("x=",x,"func3")
            print("y=",y,"func3")
            z = 5
            print("z=",z,"func3")
            x = 10

        func3()

    func2()
    print("x=",x,"func1")

func1()

现在,在这种情况下,不会在全局范围内声明任何变量,并且在python2中,没有(简单/干净)的方法可以在func1范围内从func3.这就是nonlocal关键字在python3.x中引入的原因. nonlocalglobal的扩展,它允许您修改从其他作用域中提取的变量,而不论该变量是从哪个作用域中提取的.

Now in this case, none of the variables are declared at the global scope, and in python2, there is no (easy/clean) way to change the value of x in the scope of func1 from within func3. That's why the nonlocal keyword was introduced in python3.x . nonlocal is an extension of global that allows you to modify a variable that you picked up from another scope in whatever scope it was pulled from.