Home > Backend Development > Python Tutorial > What are python decorators? how to use?

What are python decorators? how to use?

不言
Release: 2018-12-30 10:57:36
forward
3280 people have browsed it

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
Copy after login

What the decorator does isfunc1(func2) Pass a function object to our decorator and then execute the decoratorfunc1The 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
Copy after login

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()
Copy after login

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
Copy after login

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
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template