


What does python decorator mean? How to use python decorators?
Python---detailed explanation of decorators
Definition:
Essentially a function. Its function is to decorate another function (that is, the decorated function) and add functionality to the decorated function. The premise is that the source code and calling method of the decorated function cannot be changed. Such a function is called a decorator.
Analysis:
## Let’s talk about it below Not much to say, just explain it in code. Below is a function.
b=1+2
1 #原函数 2 def add(): 3 a=1+2 4 print(a) 5 #装饰器 6 def decorator(func): 7 def warpper(): 8 print("1+2的结果是:") 9 func()10 return warpper11 #注意此句 12 add=decorator(add)13 #调用函数14 add()
In this way we successfully achieved our goal. Pay attention to this sentence on line 12. This sentence passes the add function object into the decorator() function, and returns a new function variable. This new function object is reassigned to add, so that it can be guaranteed not to change. The calling method of the decorated function remains unchanged. There is a more elegant way to replace the statement on line 12 in Python syntax. As follows:
1 #装饰器 2 def decorator(func): 3 def warpper(): 4 print("1+2的结果是:") 5 func() 6 return warpper 7 8 #add=decorator(add) 9 #原函数10 @decorator#换成@符号11 def add():12 a=1+213 print(a)14 #调用函数15 add()
#What should I do if the decorated function has parameters?
What if the decorated function has parameters? How should we work? Don't worry, we can collect parameters in the form of indefinite parameters. The example code is as follows:1 def decorator(func): 2 def warpper(*args,**kwargs): 3 print("相加的结果是:") 4 func(*args,**kwargs) 5 return warpper 6 7 @decorator 8 def add(x,y): 9 a=x+y10 print(a)11 12 add(2,3)
程序输出: —————————————————— 相加的结果是: 5 ——————————————————
1 def index():2 print("welcome to the index page")3 def home():4 print("welcome to the home page")5 def bbs():6 print("welcome to the bbs page")7 return "I am the return contents"
1 username,passwd="jack","abc123"#模拟一个已登录用户 2 def decorator(func): 3 def warpper(*args,**kwargs): 4 Username=input("Username:").strip() 5 password=input("Password:").strip() 6 if username==Username and passwd==password: 7 print("Authenticate Success!") 8 func(*args,**kwargs) 9 else:10 exit("Username or password is invalid!")11 return warpper12 13 def index():14 print("welcome to the index page")15 @decorator16 def home():17 print("welcome to the home page")18 @decorator19 def bbs():20 print("welcome to the bbs page")21 return "I am the return contents"22 23 index()24 home()25 bbs()
Username:jack
Password:abc123
Authenticate Success! :jack
————————
We noticed that bbs() has a return value. If we change the last sentence of the above code (line 25) to “print(bbs() )" and then look at his output:
————————
Username:jack
Password:abc123Authenticate Success!
welcome to the home pageUsername:jack
Password:abc123
Authenticate Success!
welcome to the bbs page
None Has it become None? ? ?
————————
What happened! The return value of bbs() is printed as None. how so? Wouldn't this change the source code of the decorated function? How can this be solved? Let’s analyze it:
When we execute the bbs function, it is actually equivalent to executing the wrapper function in the decorator. After careful analysis of the decorator, we found that the wrapper function has no return value, so in order to let it To ensure that the return value of the decorated function can be returned correctly, the decorator needs to be modified:
1 username,passwd="jack","abc123"#模拟一个已登录用户 2 def decorator(func): 3 def warpper(*args,**kwargs): 4 Username=input("Username:").strip() 5 password=input("Password:").strip() 6 if username==Username and passwd==password: 7 print("Authenticate Success!") 8 return func(*args,**kwargs)#在这里加一个return就行了 9 else:10 exit("Username or password is invalid!")11 return warpper12 13 def index():14 print("welcome to the index page")15 @decorator16 def home():17 print("welcome to the home page")18 @decorator19 def bbs():20 print("welcome to the bbs page")21 return "I am the return contents"22 23 index()24 home()25 bbs()
如图加上第8行的return就可以解决了。下面我们在看看改后的程序输出:
————————
welcome to the index page
Username:jack
Password:abc123
Authenticate Success!
welcome to the home page
Username:jack
Password:abc123
Authenticate Success!
welcome to the bbs page
I am the return contents #bbs()的返回值得到了正确的返回
——-——————
好了,返回值的问题解决了.
既然装饰器是一个函数,那装饰器可以有参数吗?
答案是肯定的。我们同样可以给装饰器加上参数。比如还是上面的三个页面函数作为例子,我们可以根据不同页面的验证方式来给程序不同的验证,而这个验证方式可以以装饰器的参数传入,这样我们就得在装饰器上在嵌套一层函数 了:
1 username,passwd="jack","abc123"#模拟一个已登录用户 2 def decorator(auth_type): 3 def out_warpper(func): 4 def warpper(*args,**kwargs): 5 Username=input("Username:").strip() 6 password=input("Password:").strip() 7 if auth_type=="local": 8 if username==Username and passwd==password: 9 print("Authenticate Success!")10 return func(*args,**kwargs)11 else:12 exit("Username or password is invalid!")13 elif auth_type=="unlocal":14 print("HERE IS UNLOCAL AUTHENTICATE WAYS")15 return warpper16 return out_warpper17 18 def index():19 print("welcome to the index page")20 @decorator(auth_type="local")21 def home():22 print("welcome to the home page")23 @decorator(auth_type="unlocal")24 def bbs():25 print("welcome to the bbs page")26 return "I am the return contents"27 28 index()29 home()30 bbs()
输出:
————————
welcome to the index page
Username:jack
Password:abc123
Authenticate Success!
welcome to the home page
Username:jack
Password:abc123
HERE IS UNLOCAL AUTHENTICATE WAYS
————————
可见,程序分别加入了第2行和第16行和中间的根据auth_type参数的判断的相关内容后, 就解决上述问题了。对于上面的这一个三层嵌套的相关逻辑,大家可以在 pycharm里头加上断点,逐步调试,便可发现其中的道理。
总结
要想学好迭代器就必须理解一下三条:
1.函数即变量(即函数对象的概念)
2.函数嵌套
3.函数式编程
The above is the detailed content of What does python decorator mean? How to use python decorators?. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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



Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of the mode function in C++ In statistics, the mode refers to the value that appears most frequently in a set of data. In C++ language, we can find the mode in any set of data by writing a mode function. The mode function can be implemented in many different ways, two of the commonly used methods will be introduced in detail below. The first method is to use a hash table to count the number of occurrences of each number. First, we need to define a hash table with each number as the key and the number of occurrences as the value. Then, for a given data set, we run

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Detailed explanation of the remainder function in C++ In C++, the remainder operator (%) is used to calculate the remainder of the division of two numbers. It is a binary operator whose operands can be any integer type (including char, short, int, long, etc.) or a floating-point number type (such as float, double). The remainder operator returns a result with the same sign as the dividend. For example, for the remainder operation of integers, we can use the following code to implement: inta=10;intb=3;

Detailed explanation of the usage of Vue.nextTick function and its application in asynchronous updates. In Vue development, we often encounter situations where data needs to be updated asynchronously. For example, data needs to be updated immediately after modifying the DOM or related operations need to be performed immediately after the data is updated. The .nextTick function provided by Vue emerged to solve this type of problem. This article will introduce the usage of the Vue.nextTick function in detail, and combine it with code examples to illustrate its application in asynchronous updates. 1. Vue.nex

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

PHP-FPM is a commonly used PHP process manager used to provide better PHP performance and stability. However, in a high-load environment, the default configuration of PHP-FPM may not meet the needs, so we need to tune it. This article will introduce the tuning method of PHP-FPM in detail and give some code examples. 1. Increase the number of processes. By default, PHP-FPM only starts a small number of processes to handle requests. In a high-load environment, we can improve the concurrency of PHP-FPM by increasing the number of processes

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions
