Home > Article > Backend Development > What are python decorators? how to use?
This article brings you what is a python decorator? how to use? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
In the process of learning python, I believe everyone has a vague concept of decorators. I was also confused for a long time and decided to write an article to sort it out.
First of all, we must understand what a decorator is:
Simply speaking, a decorator can be understood as a function that wraps a function, adding functions when the function is running. It does not affect the original content of this function, and you can also perform cleanup work after the function is executed.
Give a small example
@func1 def func2(): pass
What the decorator does isfunc1(func2)
Pass a function object to our decorator and then execute the decoratorfunc1
The content inside it, and then execute the function func2
.
For example, take an example of a common decorator
#普通装饰器 def func1(func): def add_func(): print('这是我要添加的功能') return func() return add_func @func1 def func2(): print('hello world') #func1装饰器函数 #func2被装饰的函数 >>>func2() 这是我要添加的功能 hello world
In this code, func2() is equivalent to func1(func2)()->add_func(), and then you want to execute func2( ) function, the decorator func1() function is executed first, and the parameter func is equivalent to func2(). When executing the func1 function, since the return value is add_func, we execute func1(func2)(), which is equivalent to executing add_func(), outputting
'This is the function I added', and returning a func() function. That is, the call of parameter func, that is, the call of the corresponding func2 function.
Basic framework of decorator
def func1(func): def func2(): return func() return func1()
Advanced
Let’s create a decorator with parameters
def func1(func): def func2(a,b): a=1 b=2 return func(a,b) return func() @func1 def func(x,y) print('this is a add func') print(x+y) >>>func(10,20) this is a add func 3
Since the parameters of the decorated function in this code are passed to the variables of the decorated function, that is, x
is passed to a
, y
is passed to b
, return func(a,b)
means that the func
function is called and the parameters of the function are a
and b
.
A more advanced decorator function with parameters (haha, are you feeling a little confused?)
def arg_func(arg): def _func(func): def _func1(): if arg=='good': print('出去玩') if arg=='bad': print('不出去玩') return func() return _func1 return _func @arg_func('bad') def func(): print('bad day') @arg_func('good') def func1() print('good day') >>>func() 不出去玩 bad day >>>func1() 出去玩 good day
This code is actually very simple, that is, the decorator has one more parameter for judgment. It seems easy to understand. Similarly, when the func decorator is executed, the parameter of the function (arg_func) is bad, and when the func1 function is executed, the parameter of the decorator function is good
The above is the detailed content of What are python decorators? how to use?. For more information, please follow other related articles on the PHP Chinese website!