In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use the decorator of python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the decorator of python.
1. Realize the function of astatb.
Def A (a, b): print (a + b) return a + bif _ _ name__ = "_ _ main__": a (1,2)
2. Now I want to realize the function of a+2+b*2 with the same function A-- the function takes parameters or the number of parameters is uncertain.
Def B (fun): def C (* args): a = args [0] + 2 b = args [1] * 2 return fun (ameme b) return C@Bdef A (a name__ b): print (a + b) return a + bif _ name__ = = "_ main__": a (1, 2) output result: 7
(1) when the main function runs A (1Magne2), enter the decorator B first, and pass the parameter an args to * args (where args can represent an unlimited number of variable inputs); after modifying the parameter a stroke b, finally enter the A function for calculation.
(2) the input parameter of function B is fun, and the input parameter of function C is args, and the corresponding actual value is A (), (aline b).
3. After adding the decorator, I want to continue to use the original function function-decorator with parameters
Def D (flag): def B (fun): def C (* args): if flag: a = args [0] + 2 b = args [1] * 2 return fun (a, b) else: return fun (* args) return C return B
@ D (flag=False) def A (a, b): print (a + b) return a + b
If _ _ name__ = = "_ _ main__": a (1,2)
Output: 3
Through the judgment of the flag field, we can realize the original function of A () or the additional function of A.
It should be noted here that another layer of function D is added to the original decorator B function. In order to deepen the impression, it can be understood as follows:
D is for the flag parameter, B is for the A () parameter, and C is for the a max b parameter. The corresponding order is based on the context of the flag/A/ab.
4. After writing the function D, I find that I want to add a new function, what should I do? Decorators are stacked.
# coding=utf-8# @ Auther: "excellent Pengge Thieves" # @ Date: 2019 @ Software: PyCharmdef D (flag): def B (fun): def C (* args): print ("I am D decorator") print ("the current input value is changed to:" * args) if flag: a = args [0] + 2 b = args [1] * 2 return fun (a, b) else: return fun (* args) return C return B
Def E (fun): def F (* args): print ("I am the E decorator") print ("the current input value becomes:", * args) a = args [0] + 10b = args [1] + 10 return fun (a, b) return F
D (flag=True) @ Edef A (a, b): print (a + b) return a + b
If _ _ name__ = = "_ _ main__": a (1,2)
The output result is: I am D decorator current input value content becomes: 1 2 I am E decorator current input value content becomes: 3 427
When multiple decorators appear, the corresponding functions are calculated respectively in order, so according to the above example code, the calculation of the decorator D is carried out first, and the a _ hand b is changed into a 3max _ 4; then, the calculation of the decorator E is entered, and the a _ stroke b is changed into 13Compare 14, finally the A () function is entered, and the output 27 is output.
Thank you for your reading, the above is the content of "how to use the python decorator". After the study of this article, I believe you have a deeper understanding of how to use the python decorator, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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: 293
*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.