Python非本地关键字
Python nonlocal 关键字用于创建引用最近范围内的变量的变量。它绑定到的变量的范围不应是全局或局部范围。在Python,非局部变量遵循与相同绑定不同的行为,它在另一个范围内搜索变量。非局部变量用于嵌套函数。非局部变量用于声明变量。
非本地化的优势:
- 它有助于访问不在同一范围内的变量。
- 由于它使一个变量引用另一个变量,因此它使变量更可重用并为新变量节省内存。
非本地的缺点:
- 非局部变量不与全局和局部变量一起使用。
- 非局部变量不用于不在嵌套范围内的变量。
演示非局部变量:
示例 1:在本示例中,我们演示了非局部变量的工作。我们创建 nonlcoal 变量并在嵌套范围内更改其内容,并影响上层范围内的变量内容。
Python3
# Python code to demonstrait
# nonlocal keyword
# Nested function to demonstrait
# nonlocal keyword
def geek_func():
# local variable to geek_func
geek_name = "geek"
# Inner function
def geek_func2():
# Declairing nonlocal variable
nonlocal geek_name
geek_name = 'GeekForGeeks'
# Printing our variable
print(geek_name)
# Calling inner function
geek_func2()
# Printing local variable
print(geek_name)
geek_func()
Python3
# Python code to demonstrait
# nonlocal keyword
# Nested function to demonstrait
# nonlocal keyword
# Declairing variable in global scope
geek_name = 'geekforgeeks'
def geek_func():
# Defining inner function
def geek_func2():
# Declairing nonlocal variable
nonlocal geek_name
geek_name = 'GeekForGeeks'
# Printing our variable
print(geek_name)
# Calling inner function
geek_func2()
geek_func()
Python3
# Python code to demonstrait
# nonlocal keyword
# Nested function to demonstrait
# nonlocal keyword
def geek_func():
# local variable to geek_func
geek_name = "geekforgeeks"
# First Inner function
def geek_func1():
geek_name = "GeekforGeeks"
# Second Inner function
def geek_func2():
# Declairing nonlocal variable
nonlocal geek_name
geek_name = 'GEEKSFORGEEKS'
# Printing our nonlocal variable
print(geek_name)
# Calling Second inner function
geek_func2()
# Calling First inner function
geek_func1()
# Printing local variable to geek_func
print(geek_name)
geek_func()
输出:
GeekForGeeks
GeekForGeeks
示例 2:在这个示例中,我们看到当我们创建一个非局部变量来引用全局变量时会发生什么。
蟒蛇3
# Python code to demonstrait
# nonlocal keyword
# Nested function to demonstrait
# nonlocal keyword
# Declairing variable in global scope
geek_name = 'geekforgeeks'
def geek_func():
# Defining inner function
def geek_func2():
# Declairing nonlocal variable
nonlocal geek_name
geek_name = 'GeekForGeeks'
# Printing our variable
print(geek_name)
# Calling inner function
geek_func2()
geek_func()
输出:
SyntaxError: no binding for nonlocal 'geek_name' found
非局部变量的可能应用是当访问另一个作用域的变量而不创建新变量时,它引用一个最近绑定到它的变量。非本地关键字有很多可能的应用。
示例 3:在这个示例中,当我们有多个嵌套函数并且它们都有一个同名的变量时,我们将看到哪个变量 nonlocal 引用。
蟒蛇3
# Python code to demonstrait
# nonlocal keyword
# Nested function to demonstrait
# nonlocal keyword
def geek_func():
# local variable to geek_func
geek_name = "geekforgeeks"
# First Inner function
def geek_func1():
geek_name = "GeekforGeeks"
# Second Inner function
def geek_func2():
# Declairing nonlocal variable
nonlocal geek_name
geek_name = 'GEEKSFORGEEKS'
# Printing our nonlocal variable
print(geek_name)
# Calling Second inner function
geek_func2()
# Calling First inner function
geek_func1()
# Printing local variable to geek_func
print(geek_name)
geek_func()
输出:
GEEKSFORGEEKS
geekforgeeks