Python教程-如何在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'
为了使上述应用程序运行,我们必须添加Python关键字"global"。现在让我们学习全局变量。
全局关键字
在函数内部分配值给或更改在全局作用域中声明的变量时,我们必须使用全局Python关键字。不需要使用该关键字来显示或访问全局作用域变量。如果在函数内部声明变量而未使用全局关键字将其声明为全局变量,则假设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