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!

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

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...


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

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!