python全局变量_Python全局变量

本文介绍了Python全局变量的概念和使用。全局变量是在函数外部声明的,可用于多个函数间共享数据。在Python中,试图在函数内部改变全局变量的值需要使用`global`关键字,否则会引发`UnboundLocalError`。通过示例展示了如何正确使用全局变量,强调了全局变量的使用风险及其处理方式。
摘要由CSDN通过智能技术生成

python全局变量

In this tutorial we will be discussing about Python global variables.

在本教程中,我们将讨论Python全局变量。

A variable which is declared outside of any function i.e. in the global space is known as global variable.

在任何函数外部(即在全局空间中)声明的变量称为全局变量。

Global variables are useful when multiple functions need to use the same data. In C, C++ we can use and modify global variables directly in local functions and if we declare a local variable with the same name of global variable then it shadows the global variable but this is not the case in Python.

当多个功能需要使用相同的数据时,全局变量很有用。 在C,C ++中,我们可以直接在局部函数中使用和修改全局变量,如果我们声明一个具有相同全局变量名称的局部变量,则它会掩盖全局变量,但在Python中并非如此。

Let’s see this with the help of one example :

让我们借助一个示例来了解这一点:

def foo():
    print(x)
    
x = 5
foo()
print(x)

The output of the above program is:

上面程序的输出是:

5 5

5 5

Here x is global variable and it can be accessed in function and the program works correctly but what if we try to change the variable value inside the function.

这里x是全局变量,可以在函数中访问它,程序可以正常运行,但是如果我们尝试在函数内部更改变量值,该怎么办。

def foo():
    x = x+2
    print(x)
    
x = 5
foo()
print(x)

When we run the above code, we will get the below error:

当我们运行上面的代码时,我们将得到以下错误:

UnboundLocalError: local variable ‘x’ referenced before assignment

UnboundLocalError:分配前已引用局部变量“ x”

Here the problem is x in the function is now being treated as a local variable.

这里的问题是函数中的x现在被视为局部变量

In python if it is just reading the variable and the variable doesn’t exist locally then it will try to look into any containing scope (Global scope in our first example). But this is not the case when we are trying to change the variable value. Python assumes that any variable whose value is being changed in any function is local to the function unless explicitly told.

在python中,如果它只是读取变量而该变量在本地不存在,则它将尝试查看任何包含范围(在我们的第一个示例中为Global范围)。 但是,当我们尝试更改变量值时,情况并非如此。 Python假定,除非明确告知,否则在任何函数中其值都将被更改的任何变量都是该函数的局部变量。

To understand the above statement clearly let’s see one more example :

为了清楚地理解上述陈述,让我们再看一个示例:

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

The output of the above code is:

上面代码的输出是:

5 2 5

5 2 5

If you are familiar with C or C++ you may expect:

如果您熟悉C或C ++,则可能会期望:

5 2 2

5 2 2

As your output but in Python the x declared inside the function is treated as local variable and has no relation with the x declared outside the function.

作为输出,但在Python中,在函数内部声明的x被视为局部变量,并且与在函数外部声明的x没有关系。

To use the x as global variable in foo we should use global keyword, x inside the function will then be treated as a global variable.

要在foo中使用x作为全局变量,我们应该使用global关键字,然后将函数内部的x视为全局变量。

Here is an example:

这是一个例子:

def foo():
    global x
    x = 2   
    print(x)
    
x = 5
print(x)
foo()
print(x)

The output here is:

输出是:

5 2 2

5 2 2

Using the global keyword we can make the function use the global copy of x and therefore any changes made to the function are reflected globally in x.

使用global关键字,我们可以使函数使用x的全局副本,因此对函数所做的任何更改都将全局反映在x中

Why global keyword?

为什么要使用全局关键字?

The global keyword is used because global variables are dangerous and are difficult to handle. Python wants to make sure that you really want to use global variable inside the function and therefore the global keyword is being used.

使用global关键字是因为全局变量很危险且难以处理。 Python希望确保您确实要在函数内部使用全局变量,因此正在使用global关键字。

Let’s take an example to cover all the above points.

让我们以一个示例来涵盖以上所有方面。

def hoo():
    print(x)
 
def boo():
    x = 6
    print(x)
    
 
def foo():
    global x
    x = 7
    print(x)
    
x = 5
hoo()  # print 5 
boo()  # print 6
hoo()  # print 5
foo()  # print 7
print(x) # print 7
hoo()  # print 7
boo()  # print 6

The output of the above program is:

上面程序的输出是:

5 6 5 7 7 7 6

5 6 5 7 7 7 6

Lets try to understand above code.

让我们尝试理解上面的代码。

  • First call to hoo prints the global variable as it is just reading the variable x.

    第一次调用hoo会打印全局变量,因为它只是在读取变量x。

  • Calling boo will create a local copy of x inside the function and initialize it with 6 and therefore it will print 6. After the call to function is finished the local variable is discarded. This function will not affect global x.

    调用boo将在函数内部创建x的本地副本,并用6对其进行初始化,因此它将打印6。在对函数的调用完成之后,将丢弃本地变量。 此函数不会影响全局x

  • As x is unaffected calling hoo again will print the global value of x i.e. 5

    由于x不受影响,再次调用hoo将输出x的全局值,即5

  • foo is using the global keyword and therefore it will now use the global copy of x and change made will directly change global x.

    foo使用的是global关键字,因此它现在将使用x的全局副本,所做的更改将直接更改global x

  • After the call to foo is executed value of global x is changed to 7 so print(x) will print 7.

    执行对foo的调用后,全局x的值更改为7,因此print(x)将输出7。

  • hoo again will simply print the global value of x i.e. 7

    hoo将再次简单地打印x的全局值,即7

  • Call to boo will create a local variable x with value 6 and will print it and global x will remain unchanged.

    调用boo将创建一个值为6的局部变量x ,并将其打印出来,而全局x将保持不变。

I hope now you got the idea about python global variables. Comment down below if you still have any queries.

我希望现在您对python全局变量有所了解。 如果您还有任何疑问,请在下面注释掉。

翻译自: https://www.thecrazyprogrammer.com/2019/08/python-global-variables.html

python全局变量

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值