In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "what is the role of decorators in python", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "what is the role of decorators in python" this article.
Python decorator
Python decorator is used to expand the function of the original function, the special feature of this function is that its return value is also a function, the advantage of using python decorator is to add new functions to the function without changing the code of the original function.
Generally speaking, if we want to expand the original function code, the most direct way is to invade and modify the original function code, but the simplest way is not the safest and most appropriate way, according to the principle of extension, open modification and closure. It is not advisable to modify the original function code. For example, there is a case below:
Company A has a core interface department M that provides core business interfaces, and three business units (NMagneWMagazine Z) need to call core business interfaces. After a year of normal operation, company specification interface calls need to verify user permissions for interface calls. Now we need to achieve this extension function at the lowest cost.
The sample code is as follows:
# Core Interface Department # def F1 (): print ('f1') def f2 (): print (' f2') def f3 (): print ('f3') def f4 (): print (' f4') # Business Unit N calls the core interface provided by Function # F1 () f2 () f3 () f4 () # Business Unit W invokes the function provided by the core interface # F1 () f2 () f3 () f4 () # Business Unit Z calls the function provided by the core interface Function # F1 () f2 () f3 () f4 (). The core interface department reconstructs the original function Add verification code # core interface department # def verify (): print ('verification code') def F1 (): verify () print ('f1') def f2 (): verify () print (' f2') def f3 (): verify () print ('f3') def f4 (): verify () print (' f4')
Analysis: violates the principle of opening and closing! It is open to extension and closed to modification. After the core function is written, it is not allowed to enter the function to modify it. If you have 1000 interfaces, you have to change them a thousand times. Although you have no feelings for the caller, the core interface department is very expensive.
The core interface department extends the authentication interface Business unit verifies when calling # core interface department # def verify (): print ('authentication code') def f1 (): print ('f1') def f2 (): print (' f2') def f3 (): print ('f3') def f4 (): print (' f4') # # Business Unit N calls the function provided by the core interface # if (verify ()) {F1 () f2 () f3 () f4 ()} else {print ('not verified')} # Business unit W invokes the function provided by the core interface # # if (verify ()) {F1 () f2 () f3 () f4 ()} else {print ('not verified')} # Business Unit Z calls the function provided by the core interface # if (verify ()) {F1 () f2 () f3 () f4 ()} else {print ('failed authentication')}
Analysis:
The above code meets the principle of open and closed. The core interface department only needs to extend a verification function, but the business calling department needs to call the verification function, which is not conducive to the use of callers.
Core interface decorator for verification Business Unit has no feeling # Core Interface Unit # def verify (func): def inner (): # Verification 1 # Verification 2 # Verification 3 return func () return inner@verifydef F1 (): print ('f1') @ verifydef f2 (): print (' F2') @ verifydef f3 (): print ('f3') @ verifydef f4 (): print (' f4')
Analysis:
The above decorator solves the verification problem, as long as the core interface department operates and meets the open and closed principle. Take F1 as an example, execute the verify function and take the function under @ verify as the argument to the verify function, that is, @ verify is equivalent to verify (F1), so it will be executed internally: def inner: # verifies that return F1 () # func is the parameter, and the inner,inner returned by func equals f1return inner # represents the function A non-executing function is actually stuffing the original F1 function into another function and assigning the return value of the executed verify function to the function name of the function below @ verify. The return value of the verify function is: def inner: # verify the original F1 () # the F1 here represents the original F1 function, and then re-assign the return value to F1. That is, the new F1 = def inner: # verifies the original F1 (). So, when the business unit wants to execute the F1 function in the future, it will execute the new F1 function, execute the verification inside the new F1 function first, then execute the original F1 function, and then return the return value of the original F1 function to the business caller. In this way, the verification function is performed, the contents of the original F1 function are executed, and the return value of the original F1 function is returned to the business call. Expansion
What we wrote above has no parameters, how do we write with parameters?
With one parameter def verify (func): def inner (arg1): # Verification 1 # Verification 2 # Verification 3 return func (arg1) return inner@verifydef F1 (arg1): print ('F1')
Note: if you want to specify the number of parameters, you can add the specified number of parameters to it. As follows:
With two parameters def verify (func): def inner (arg1,arg2): # verification 1 # verification 2 # verification 3 return func (arg1,arg2) return inner@verifydef F1 (arg1,arg2): print ('f1') with N parameters def verify (func): def inner (* args * * kwargs): # Verification 1 # Verification 2 # Verification 3 return func (* args,**kwargs) return inner@verifydef F1 (arg1,arg2,arg3,arg4,arg5): print ('f1') A function can have multiple decorators def W1 (func): def inner (* args) * * kwargs): # Verification 1 # Verification 2 # Verification 3 return func (* args,**kwargs) return innerdef w2 (func): def inner (* args,**kwargs): # Verification 1 # Verification 2 # Verification 3 return func (* args,**kwargs) return inner@w1@w2def F1 (arg1,arg2 Arg3): the above print'F1'is all the content of the article "what are the functions of decorators in python"? Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.