


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+2Program output: ————————3————————Now I want to add an explanatory sentence to this function, as follows, we can write a decorator:
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()Program output: ——————————The result of 1+2 is: 3——————————
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()Add "@xxx" directly in front of the decorated function (xxx is the decorator function name)
#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"If we now want to add verification to the home page and bbs page, obviously it is not feasible to change the source code now. At this time we can use the decorator, as follows:
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()Program result: ————————welcome to the index page #You can log in directly to the index page without verification
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!

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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