Python The decorators in are implemented by closures that take advantage of function properties , So before we talk about decorators , We need to understand the properties of functions first , And how closures take advantage of function properties
python In general, there are four characteristics of functions in :
def add(x):
return x + 1
a = add # As a variable
explain : Function without parentheses , It's not going to work , Represents a function object , It can be passed as a variable
def add(x):
return x + 1
def execute(f):
return f(3)
execute(add) # As a parameter
explain : A function can accept another function object as its own parameter , And handle the function object
def add(x):
return x + 1
def get_add():
return add # As return value
explain : The return value of a function can be another function object
def outer():
x = 1
def inner():
print(x) # Nested function inner Inside x Variables can be obtained from the encapsulation domain
inner()
outer()
explain : A function ( The main function ) Inside, you can nest another function ( Subfunctions ) Of , such as outer Functions are nested from the inside inner. A variable that does not exist in the local domain of a function , It can span its encapsulation domain ( The range between the main function and the sub function ) To find
python Decorators in are implemented through closures , Simply speak , A closure is an internal function that references an external variable , The implementation of closures makes use of the above function features , Let's take a look at how closures are implemented :
def outer(x):
def inner(): # Nested function
return x # Cross domain access , External variables are referenced x
return inner # Function as return value
closure = outer(' External variables ') # Function is assigned to as a variable closure
print(closure()) # Execute closure
Execution results :
External variables
explain : Let's analyze this process ,outer Received ' External variables ', Pass to inner, As it return Parameters of , Last outer return inner function , Back to inner Function is passed as a variable to closure, Finally, execute closure This function object , In fact, it was carried out inner This function , Back to ' External variables ', This implements a simple closure
We found that the above closure example only uses one of the above 3 Function features , Function as parameter This feature doesn't seem to work , Don't worry. , Let's go step by step , Just imagine ,outer Parameters of x Can it also be a function object ?
Let's rewrite the code to implement closures :
def func():
return ' function func'
def outer(x):
def inner(): # Nested function
return ' Yes inner Brand hat ' + x() # Cross domain access , External variables are referenced x
return inner # Function as return value
closure = outer(func) # function func As outer Parameters of ; Function is assigned to as a variable closure
print(func()) # Execute the original function
print(closure()) # Execute closure
Execution results :
function func
Yes inner Brand hat function func
explain : We see the printed results , from func() To closure(), Are we feeling functions func It was decorated , Turned into closure, How did you decorate it ?
Here's the point !!!!!!!!!!!
We see closure It's actually outer(func),func Pass in as a parameter outer,outer Subfunction of inner Yes func The returned results are decorated , Returns a decorated result , Last outer return inner, so to speak inner It is decorated func, This is the process of decorating a function , The focus is on implementation outer(func) This step
python It provides us with grammar sugar @, We want to execute outer(func) When , Only need to outer function @ To func Just above the function
The specific implementation is as follows :
def outer(x):
def inner():
return ' Yes inner Brand hat ' + x()
return inner
@outer
def func():
return ' function func'
print(func())
Execution results :
Yes inner Brand hat function func
explain : We see the printed results and execute with us closure() The result is the same , Also explains added outer Decorator func Equivalent to outer(func), So we know decorators very well @ What is the role of , It is used to pass the decorated function as a parameter to the decorator function for processing , When the decorated function is finally executed , It is equivalent to executing a processed function .
That's all Python The birth process of the ornament in ......
Official account 【 Chris Handbook 】, Learn more about data