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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

Fastapi ...
