Python函数中变量作用域的工作原理是什么?
在本文中,我们将讨论Python函数中变量作用域的工作原理。
名称的作用域是程序中可以清晰访问名称(例如变量)的区域。
因此,变量作用域是我们可以访问变量的区域,如下面的示例所示-
def multiplication():
product = 7*9
在此示例中,“product”变量在函数内部创建,因此只能在函数内部访问。
通常,Python作用域概念是使用称为LEGB规则的规则来呈现,基于四个变量作用域的主要类别。它代表 –
- 本地的
-
封闭的
-
全局的
-
内置的。
让我们逐一讨论所有范围。
更多Python相关文章,请阅读:Python 教程
本地的作用域
当一个变量在函数内部定义时,它的作用域被限制在函数内部。只要函数处于运行状态(源)并且从定义时可以使用,它就存在。这意味着它的值或者甚至无法从函数外部访问。
示例
以下是本地作用域变量的示例-
def prime_numbers():
first_prime_no = 2
print("The first prime number is: ", first_prime_no)
# the driver code
prime_numbers()
print("The first prime number is: ", first_prime_no)
输出结果
通过使用函数“prime_numbers()”,我们能够打印“first_prime_no”变量的值。但是,在尝试从函数外部读取并打印同一变量时,它会产生NameError。因为“first_prime_no”是“本地”于函数,无法从函数体外部访问,如下面的输出所示-
The first prime number is: 2
Traceback (most recent call last):
File "C:\Users\Lenovo\Desktop\untitled.py", line 6, in <module>
print("The first prime number is: ", first_prime_no)
NameError: name 'first_prime_no' is not defined
全局作用域
可能这是学习最简单的作用域。当变量定义在任何函数外部时,它成为全局变量,其作用域在整个程序中的任何地方。这意味着它可以在任何函数中使用,并且可以修改全局变量的值。
示例
以下是全局作用域变量的示例-
animal = "Lion"
def animal_species():
pantheria = "Pantheria"
print('The name and species of the animal is:',animal, pantheria)
def animal_kingdom(name):
print('The name and kingdom of the animal is:',animal, name)
animal_species()
animal_kingdom("Animalia")
输出结果
以下是上面代码的输出-
The name and species of the animal is: Lion Pantheria
The name and kingdom of the animal is: Lion Animalia
封闭作用域
封闭(或非本地)作用域是仅适用于嵌套函数的特殊作用域。如果本地作用域是内部或嵌套函数,则封闭作用域是外部或封闭函数的作用域。您在封闭函数中定义的名称包含在此作用域中。
示例
在示例代码的“Lion()”函数中,使用了变量“species”。在那种情况下,它既不是本地范围也不是全局范围。术语“封闭作用域”指的是此-
def animal():
species = "Pantheria"
def Lion():
kingdom= "Animalia"
print('The species of the animal is:', species)
print('The kingdom of the animal is:',kingdom)
Lion()
animal()
输出
以下是上面代码的输出 –
动物的种类是:Pantheria
动物的王国是:Animalia
Python中的内置作用域
在Python中,它具有最广泛的使用范围。 Python自带的每个模块中保留的名称都有其自己的内置作用域。当Python无法在本地、封闭或全局作用域中找到标识符时,Python会搜索内置作用域以确定标识符是否在该处定义。
Python会首先检查本地作用域以验证哪些变量在该处定义,然后是封闭作用域,最后是全局作用域。如果无法在任何地方找到标识符,则最后将检查内置作用域。
示例
这种情况下,函数int()、print()和type()不需要在这里定义,因为它们在Python的内置作用域中定义 –
x = 17.89
int(x)
print('The value of x is:',x)
print('The type of x is:',type(x))
输出
以下是上面代码的输出 –
x的值是:17.89
x的类型是:<class 'float'>
Python中的全局关键字
通过使用global,您指令Python使用全局声明的变量而不是在本地创建一个变量。只需输入“global”和变量名就可以使用该关键字。
示例
以下是在Python中使用全局关键字的示例 –
animal = "Lion"
def animal_species(Pan):
global animal
animal = Pan
def animal_kingdom():
animalia = "Animalia"
print('The animal species and kingdom is:',animal,animalia)
animal_species('Pantheria')
animal_kingdom()
输出
现在我们已经看到了,我们可以使用定义在全局作用域中的变量“animal”使用全局关键字。因此,当我们将值“Pan”赋给变量时,我们在函数外面改变了值。
动物的种类和王国是: Pantheria Animalia
示例
以下是在Python中使用全局关键字的示例 –
animal = "Lion"
def animal_name():
animal = "Panther"
print('动物是:',animal)
animal_name()
print ('动物是:',animal)
输出
上面的代码将打印出“Panther”和“Lion”的值,因为“animal”在本地作用域变量中指的是“Panther”,在“method()”函数外的全局变量中指的是“Lion”。
动物是:Panther
动物是:Lion
Python中的非局部关键字
非局部是嵌套函数中的有用关键字。因此,该变量将被引用为最近的封闭范围内先前绑定的变量。换句话说,它会阻止变量首先绑定到本地并推动其向上移动。和全局关键字一样,语法相同。
示例
在下面的示例中,“animal”指的是函数“animal_kingdom()”中的“local variable”,方法“animal_name()”中的“nonlocal variable”和这些函数之外的“global variable”。现在使用了非局部变量 –
animal = "Lion-global variable"
def animal_name():
animal = "Panther-nonlocal variable"
def animal_kingdom():
nonlocal animal
animal = "Animalia-local variable"
print('动物是:',animal)
animal_kingdom()
print('动物是:',animal)
animal_name()
print ('动物是:',animal)
输出
在函数中,即“animal_kingdom()”,我们在“animal”变量上使用了非局部关键字。因此,当我们修改非局部变量“animal”的值时,它也会更改其值,该变量定义在方法()函数即“animal_name()”中。
The animal is: Animalia-local variable
The animal is: Animalia-local variable
The animal is: Lion-global variable
极客教程