Python语言中的变量充当各种数据值和数据结构的存储单元。当变量分配给任何 Python 对象时,它会指向该对象,因为它们是内存位置中该特定对象的引用或指针。Python编程语言不是“静态类型”的,不像其他语言,如C/C++/JAVA。变量不必在使用前声明其类型或初始值。当一个变量最初被赋予一个值时,它被认为是已经形成的。
integer = 123
floating = 1688.99
string = "Yiibai"
print("This value is stored in the variable 'integer': ", integer)
print("This value is stored in the variable 'floating': ", floating)
print("This value is stored in the variable 'string': ", string)
运行结果:
This value is stored in the variable 'integer': 123
This value is stored in the variable 'floating': 1688.99
This value is stored in the variable 'string': Yiibai
变量的作用域
Python 变量的范围是指可以在其中找到它并在必要时访问它的区域。
全局变量和局部变量
全局变量是在任何函数之外声明和定义的变量,但不特定于其中任何一个。程序的任何部分都可以使用它们。
# 全局变量
a = 122
# Accessing the global variable in the main function
print("This is in the main function: ", a)
# Defining a function that uses the global variable
def f1():
print("This is in function 'f1()': ", a)
# Defining another function that uses the global variable
def f2():
print("This is in function 'f2()': ", a)
# Calling the functions
f1()
f2()
运行结果:
This is in the main function:
122
This is in function 'f1()':
122
This is in function 'f2()':
122
假设变量在该函数的局部范围内定义,并且与全局变量具有相同的名称。在这种情况下,它将仅显示提供给该特定函数中的变量的值,而不显示全局范围内分配的值。
# 声明全局变量
a = "这是在全局范围内分配的。"
# Defining a function that has a variable having the name same as the global variable
def func():
a = "这是在方法内部定义的值。"
print(a)
# Calling the function
func()
运行结果:
这是在方法内部定义的值。
在定义函数 func()
之前,变量 a
被分配给一个字符串,该字符串声明值是“这是在全局范围内分配的”
。函数func()
的 print(a)
语句将首先在其局部范围内搜索变量 a
。由于已经有一个变量“a”分配给不同的字符串,因此该函数将打印该变量的值,并且不会进入全局范围。在这种情况下,该函数不会使用全局范围内的变量a
的值。
修改函数 func()
中变量 a
的值的结果是什么,这是下一个问题。它也会对全局变量a
产生影响吗?以下代码片段用于测试它:
# Declaring a global variable
a = "This is assigned in the global scope."
# Defining a function that has a variable having the name same as the global variable
def func():
a = "This is defined inside the function."
print("Accessed in the local scope of function: ", a)
# Calling the function
func()
# Accessing the variable a in the global scope after changing its value inside the function
print("Accessed in the global scope: ", a)
运行结果:
Accessed in the local scope of function: This is defined inside the function.
Accessed in the global scope: This is assigned in the global scope.
必须使用 Python global
关键字来使前面的程序函数。只有在进行赋值或更改变量值时,才需要在函数中使用 Python global
关键字。global
关键字对于显示和访问全局作用域中存在的变量不是必需的。为什么?由于赋值给 func()
中的变量 ‘a’,Python “假定”希望访问局部变量,这就是为什么初始打印命令在局部范围内返回变量的值。如果在函数内部修改或赋值变量而未定义为全局变量,则将其视为局部变量。下面的示例演示如何使用 global
关键字来指示 Python 解释器希望修改全局变量:
# Declaring a global variable
a = "This is assigned in the global scope."
# Defining a function that uses the global keyword before assigning a new value to the global variable
def func():
global a
a = "This is defined inside the function."
print("Accessed in the local scope of function: ", a)
# Calling the function
func()
# Accessing the variable a in the global scope after changing its value inside the function
print("Accessed in the global scope: ", a)
运行结果:
Accessed in the local scope of function: This is defined inside the function.
Accessed in the global scope: This is defined inside the function.
总结全局局部变量作用域,参考以下代码:
# Defining a variable in the global scope
a = 'in the global scope'
# This function will print the global variable because there does not exist any local variable 'a'
def func():
print('Inside func(): ', a)
# This function will print the value assigned to the local variable 'a'
# Because we are not using the global keyword
def local():
a = 'inside function local()'
print('Inside local(): ', a)
# This function will modify the global variable because we are using the global keyword
def global_():
global a
a = 'changed inside function global_()'
print('Inside global_() : ', a)
# Calling the functions
# Value of 'a' in global scope after each function call
print('global: ', a)
func()
print('global: ', a)
local()
print('global: ', a)
global_()
print('global: ', a)
运行结果如下:
global: in the global scope
Inside func(): in the global scope
global: in the global scope
Inside local():
inside function local()
global: in the global scope
Inside global_() :
changed inside function global_()
global: changed inside function global_()
nonlocal 关键字
在 Python 中,嵌套函数使用 Python nonlocal
关键字进行处理。nonlocal
关键字的作用类似于全局关键字,只是在嵌套函数的情况下,它定义了一个变量来引用在外部封闭函数中分配的变量,而不是全局变量。
# Python program to show how the nonlocal keyword works
print ("Using the nonlocal keyword before changing a:")
# Assigning 'a' in the global scope
a = 0
# Defining a function
def outer():
# Giving new value to 'a' in the local scope of the 'outer' function
# Didn't use global keyword here
a = 3
def inner():
# Defining the variable scope as the local scope of the 'outer' function
nonlocal a
# Changing the value of 'a' in the local scope of the 'inner' function
a = 14
# Printing the value of 'a' in the local scope of the 'inner' function
print("Inside 'inner' function: ", a)
# Calling the inner function
inner()
# Priting the value of 'a' inside the function
print("Inside the 'outer' function: ", a)
# Calling the main function
outer()
# Printing the value of 'a' in the global scope
print("In the global scope: ", a)
# Performing the same steps as above without using the nonlocal keyword
print ("Not using the nonlocal keyword before changing a:")
# Assigning 'a' in the global scope
a = 0
def outer():
a = 3
def inner():
# Not using the nonlocal keyword before assigning a new value to a
a = 14
print("Inside 'inner' function: ", a)
inner()
print("Inside the 'outer' function: ", a)
# Calling the main function
outer()
print("In the global scope: ", a)
运行结果:
Using the nonlocal keyword before changing a:
Inside 'inner' function:
14
Inside the 'outer' function:- 14
In the global scope:
0
Not using the nonlocal keyword before changing a:
Inside 'inner' function:
14
Inside the 'outer' function: 3
In the global scope:
0