Python Global, Local and Nonlocal variables

在这篇文章中,你将学习Python中的全局变量、本地变量 和非本地变量。

全局变量

在Python中,一个变量被声明在函数之外,或者声明在全局范围内,就是全局变量。这表明,可以在函数内部或者函数外部访问全局变量。

下面我们看看在Python中如何创建一个全局变量

案例1:创建一个全局变量

x = "global"

def foo():
    print("x inside : ",x)

foo()
print("x outside : ",x )

执行结果是: x  inside : global ,  x outside :  global

在上面的代码中,我们创建了一个 全局变量x ,并且定义了一个foo()函数来打印全局变量 x 。

最后,我们调用foo()函数来打印变量 x 的值。

但如果你想在函数中修改x的值呢?

案例2:在函数中修改全局变量

x = "global"

def foo():
    x = x * 2
    print(x)

foo()

执行结果是:UnboundLocalError: local variable 'x' referenced before assignment

结果显示,Python将 x 作为一个本地变量,并且foo()函数中没有定义变量 x。 

如果想修改x的值,可以参看 https://blog.csdn.net/shafatutu/article/details/103603608

本地变量

在函数内部声明的变量和在本地范围内声明的变量,都是本地变量

案例1:访问作用域之外的本地变量

def foo():
    y = "local"

foo()
print(y)

执行结果:NameError: name 'y' is not defined

我们试着在全局范围内访问一个本地变量,但是该本地变量只在foo()函数中或本地范围内起作用。

案例2:创建一个本地变量

def foo():
    y = "local"
    print(y)

foo()

执行结果:local

全局变量和本地变量一起使用

案例1:在一个代码中使用全局变量和本地变量

x = "global"

def foo():
    global x
    y = "local"
    x = x * 2
    print(x)
    print(y)

foo()

执行结果:global global,  local

In the above code, we declare  x as a global and  y as a local variable in the
foo() . Then, we use multiplication operator  * to modify the global variable
x and we print both  x and  y .
After calling the  foo() , the value of  x becomes  global global because we used
the  x * 2 to print two times  global . After that, we print the value of local
variable  y i.e  local .

在上面的代码中,我们创建了全局变量x  ,在foo()函数中创建了本地变量y。然后我们乘法来修改全局变量x ,并且在foo()函数中打印x 和 y。

在调用foo()函数时,变量x 的值变成了"global global",因为我们用了 x*2。

之后,我们打印本地变量y的值,是"local"

案例2:全局变量和本地变量值相同

x = 5
def foo():
x = 10
print("local x:", x)
foo()
print("global x:", x)

执行结果:  local x : 10  ,  global x : 5

在上面代码中,我们用了同样的名字 "x" 来声明全局变量和本地变量。

但我们在打印 x 的时候却得到了不同的结果,因为变量 x 被声明在两个作用域中:foo()函数的本地范围 和foo()函数之外的全局范围。

当我们在foo()函数内部打印变量x的值时,结果是 "local x : 10",这是本地变量的值。

当我们在foo()函数外打印变量x的值时,结果是"global x : 5",这是全局范围内的变量的值。

非本地变量

Nonlocal variable are used in nested function whose local scope is not de?ned. This
means, the variable can be neither in the local nor the global scope.
Let's see an example on how a global variable is created in Python.
We use  nonlocal keyword to create nonlocal variable.

非本地变量(非局部变量)用在嵌套的函数中,该函数的局部作用域(local scope)没有被定义。也就是说,该变量既不是本地范围,也不是全局范围。

案例1:创建一个非局部变量

def outer():
    x = "local"

    def inner():
        nonlocal x
        x = "nonlocal"
        print("inner:", x)

    inner()
    print("outer:", x)

outer()

执行结果:inner:nonlocal  , outer:nonlocal

In the above code there is a nested function  inner() . We use  nonlocal keyword
to create nonlocal variable. The  inner() function is defined in the scope of
another function  outer() .
Note : If we change value of nonlocal variable, the changes appears in the local
variable.

在上面的代码中,有一个嵌套函数inner()。

我们用nonlocal关键字声明了一个非局部变量。

inner()函数被声明在outer()函数的作用域之内。

Note:如果我们改变非局部变量的值,这些变化会出现在局部变量。(没明白为什么,,)

发布了57 篇原创文章 · 获赞 20 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/shafatutu/article/details/103607907