Python Advanced - Modifying External Variables Used in Closures
External Variables Introduction:
Modify external variables used inside the closure
Bad example for modifying an external variable used inside a closure:
# define an external function
def func_out (num1):
# define an inner function
def func_inner (num2):
# The original intention here is to modify the value of the external num1, in fact, a local variable num1 is defined in the internal function
num1 = 10
# The inner function uses the outer function's variable (num1)
result = num1 + num2
print("The result is: ", result)
print(num1)
func_inner ( 1 )
print(num1)
# The outer function returns the inner function, and the inner function returned here is the closure
return func_inner
# create the closure instance
f = func_out ( 1 )
# execute the closure
f( 2)
Bad example for modifying an external variable used inside a closure:
# define an external function
def func_out (num1):
# define an inner function
def func_inner (num2):
# The original intention here is to modify the value of the external num1, in fact, a local variable num1 is defined in the internal function
nonlocal num1 # Tell the interpreter that the external variable a is used here
# Modify the external variable num1
num1 = 10
# The inner function uses the outer function's variable (num1)
result = num1 + num2
print("The result is: ", result)
print(num1)
func_inner ( 1 )
print(num1)
# The outer function returns the inner function, and the inner function returned here is the closure
return func_inner
# create the closure instance
f = func_out ( 1 )
# execute the closure
f( 2)
•Modifying external function variables used inside a closure is done using the nonlocal keyword.
Modify external variables used inside the closure
1. External Variables .Modify the external variables used in the closure
Bad example for modifying an external variable used inside a closure:
# define an external function
def func_out (num1):
# define an inner function
def func_inner (num2):
# The original intention here is to modify the value of the external num1, in fact, a local variable num1 is defined in the internal function
num1 = 10
# The inner function uses the outer function's variable (num1)
result = num1 + num2
print("The result is: ", result)
print(num1)
func_inner ( 1 )
print(num1)
# The outer function returns the inner function, and the inner function returned here is the closure
return func_inner
# create the closure instance
f = func_out ( 1 )
# execute the closure
f( 2)
Bad example for modifying an external variable used inside a closure:
# define an external function
def func_out (num1):
# define an inner function
def func_inner (num2):
# The original intention here is to modify the value of the external num1, in fact, a local variable num1 is defined in the internal function
nonlocal num1 # Tell the interpreter that the external variable a is used here
# Modify the external variable num1
num1 = 10
# The inner function uses the outer function's variable (num1)
result = num1 + num2
print("The result is: ", result)
print(num1)
func_inner ( 1 )
print(num1)
# The outer function returns the inner function, and the inner function returned here is the closure
return func_inner
# create the closure instance
f = func_out ( 1 )
# execute the closure
f( 2)
2. External Variables Summary
•Modifying external function variables used inside a closure is done using the nonlocal keyword.
Related Articles
-
A detailed explanation of Hadoop core architecture HDFS
Knowledge Base Team
-
What Does IOT Mean
Knowledge Base Team
-
6 Optional Technologies for Data Storage
Knowledge Base Team
-
What Is Blockchain Technology
Knowledge Base Team
Explore More Special Offers
-
Short Message Service(SMS) & Mail Service
50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00