如何在Python中声明全局变量
在这个教程中,我们将学习如何在Python程序中声明全局变量。
什么是全局变量
全局变量是在任何函数内部和外部都可用的变量,只要它们是在函数外部定义的(即全局作用域)。让我们来探讨一下如何创建全局变量。
在这个示例中,我们将定义并访问一个全局变量。
示例:
# Simple Python program to show how to define a global variable
# Here, we are defining a function
def func():
print( "Inside the defined function the string is: ", var )
# Here, we are printing the local scope variable
# Here, we are declaring the global scope
var = "Declaring the Global Variable"
# Here, we are calling the function
func()
print("Outside the defined function the string is: ", var)
# Here, we are printing the global scope variable
输出:
Inside the defined function the string is: Declaring the Global Variable
Outside the defined function the string is: Declaring the Global Variable
访问全局变量
变量var在函数func()内外均被访问到,由于它在全局范围内声明,所以被指定为全局变量。
由于我们没有创建局部变量,全局变量的值将被应用;然而,我们必须确保局部变量和全局变量的名称匹配。
如果一个与标识符相同的变量在全局范围内和一个已定义的函数内被初始化,会发生什么呢?如果我们在函数func()内修改一个变量的值会发生什么?问题是改变局部变量是否会改变全局变量,或者反过来,改变全局变量是否会改变局部变量。下面的代码示例用于评估它:
示例:
# Simple Python program to show how to access the global and the local variables
# Here, we are defining a function
def func():
var = "Global Variable" # here, we have declared the variable in the local scope
print("The local scope variable is:", var )
# here, we are printing the local scope variable
# Here, we have declared the variable in the global scope
var = "I am learning Python"
func() # Here, we are calling the function
print("The global scope variable is:", var )
# here, we are printing the global scope variable
输出:
The local scope variable is: Global Variable
The local scope variable is: I am leaning Python
假设一个变量在全局范围内被赋予了一个名字,并且在函数的局部范围内被声明。在这种情况下,它只会显示在定义函数内提供的值,而不是全局值。
在局部范围内改变全局变量
如果我们试图在函数内修改全局范围内定义的变量的值会发生什么?让我们通过下面的示例来进行分析。
示例:
# Here, we will try to change the value of the variable defined in the global scope in # the local scope
# Here, we are defining a function
def func():
var = var + "Global Variable"
print("Inside the defined function", var )
# Here, we are declaring a variable in the global scope
var = "Python Tutorial"
func() # Here, we are calling the function
输出:
4 def func():
----> 5 var = var + "Global Variable"
6 print( "Inside the defined function", var )
UnboundLocalError: 在分配变量之前已引用本地变量 ‘var’
我们必须在前面的应用程序中包含”global” Python关键字才能运行。现在让我们来研究全局变量。
全局关键字
当给在函数内部全局范围内声明的变量赋值或更改其值时,我们必须使用全局的Python关键字。关键字不是显示或访问全局范围变量所必需的。由于我们在函数func()内部声明了var变量,Python解释器”假定”我们想要一个本地变量,这就是为什么第一句会引发错误。如果在函数内部修改或声明变量而不使用全局关键字将其声明为全局变量,那么它被视为本地变量。下面的示例演示了如何使用关键字”global”来指示Python解释器我们希望访问全局变量。
示例:
# Simple Python program to show the use of the global keyword to modify a variable, # defined in global scope, inside a local scope.
# Here, we are defining a function
def func():
global var # here, we are declaring a global variable
var = var + " " + "Global Variable"
print( "Inside the defined function: ", var )
# Here, we are declaring a variable in the global scope
var = "Python Tutorial"
func() # here, we are calling the function
print( "Outside the function: ", var )
# Here, the above will show if the variable var is changed in global scope also or not
输出:
Inside the defined function: Python Tutorial Global Variable
Outside the function: Python Tutorial Global Variable
显示使用局部变量和全局变量的示例
示例
# Simple Python program to show how to use global and local variables
var = 10 # here, we are declaring the variable
# Here, this will show the global value of var as there is no local var
def func1():
print('Inside the first function: ', var)
# Here, we are declaring a local variable named var
def func2():
var = 15
print('Inside the second function: ', var)
# Here, we are using the global keyword to modify var in global scope
def func3():
global var
var += 3
print("Inside the third function: ", var)
# Here, we are declaring the Global scope
print('global value of variable: ', var)
func1()
print('global value of variable: ', var)
func2()
print('global value of variable: ', var)
func3()
print('global value of variable: ', var)
输出:
global value of variable: 10
Inside the first function: 10
global value of variable: 10
Inside the second function: 15
global value of variable: 10
Inside the third function: 13
global value of variable: 13