Talking about the related knowledge of Python from global and local variables to decorator
- 2021-11-13 02:18:13
- OfStack
Global variables and local variables
# num1 Is a global variable
num1 = 1
# num2 Is a local variable
def func():
num2 = 2
Variables defined outside (and not inside) a function are global variables;
Variables defined in functions are local variables.
Local variables cannot be referenced outside functions, but global variables can be referenced inside functions. However, it should be noted that global variables cannot be modified in general functions. If global variables are modified in functions, python will automatically create a variable with the same name. Using global keywords, local variables can be changed into global variables, and then modified.
# This is a global variable
num1 = 1
# In the function num1 Is a local variable
def func1():
num1 = 2
# Call function
func1()
# Output num1 To verify that the global variable changes
print(num1)
# Output is 1
# Define local variables num2
def func2():
num2 = 2
# Try to reference a local variable outside a function
print(num2)
# Output: NameError: name 'num2' is not defined
# Referencing global variables in functions
def func3():
print(num1)
# Call function
func3()
# Output: 1
# Use global Keyword to modify local variables
def func4():
global num1
num1 = 2
# Call function
func4()
# Validation num1 Whether it has been modified
print(num1)
# Output is 2
Embedded function
# Define another within the function 1 This function is called an embedded function or an internal function
def func1():
num1 = 1
def func2():
num2 = 2
# num1 And num2 Are all local variables
# We call it func2 Is an internal function, and func1 Is an external function
# The scope of the inner function is within the whole outer function, and the inner function can refer to local variables in the outer function
LEGB Principles:
L-Local: Namespace within a function.
E-Enclosing function locals: Namespace of external functions in nested functions.
G-Global: The namespace of the module in which the function is defined.
B-Builtin: Namespace of Python built-in module.
The search order of variables is L → E → G → B.
In internal functions, only local variables of external functions can be accessed, but 1 cannot be modified. (This 1 point is similar to global variable and local variable 1), which can be modified by using nonlocal keyword
Closure
Closures in Python: Functions in Closed Environments
Definition: An internal function is considered a closure if it refers to a variable in external scope but not global scope (in short, in the context of nested functions, the internal function refers to a local variable of the external function)
Note: Because the concept of closures is derived from internal functions, internal functions cannot be called outside of them.
def func1():
num1 = 1
def func2():
print(num1)
return func2
func3 = func1()
func3()
What closures do: To avoid using global variables as much as possible, closures allow functions to be associated with some of the data (environments) they operate on, so that external functions form a closed environment for internal functions
Decorator
Decorator: In Python, the function of the decorator (decorator) is to pass the decorated function as a parameter to the function corresponding to the decorator (the function with the same name), and return the packaged decorated function.
def func1(func2):
def func3():
print(" The program begins to execute ")
func2()
print(" End of program execution ")
return func3 # ** The function must be returned, and if it is followed by parentheses, the function returned internally will be executed directly **
def func4():
print("hello world")
func4 = func1(func4)
func4()
# Output:
''' The program begins to execute
hello world
End of program execution '''
Most decorators define internal functions, but for convenience, instead of defining functions internally, you can simply have the decorator return to the original function. But when we define a function with parameters, we must define a function internally. However, it is recommended to use internal functions, because the code in internal functions will not be executed directly during the transfer process
@ Grammar Sugar
@ Syntax sugar can quickly pass the original function as an argument to an external function and return it to a new function with the same name as the original function
def func1(func2):
def func3():
print(" The program begins to execute ")
func2()
print(" End of program execution ")
return func3 # ** The function must be returned, and if it is followed by parentheses, the function returned internally will be executed directly **
@func1
def func4():
print("hello world")
func4()
For decorators, add internal function parameters [, collect parameters] to achieve more functions
There are also parameterized decorators, stacked decorators, perfect decorators, and a few built-in decorators that can be used for deeper learning