In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian for you to introduce in detail the "Python decorator application analysis", the content is detailed, the steps are clear, the details are handled properly, I hope this "Python decorator application analysis" article can help you solve your doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.
First, function review
1. In python, a function is a first-class citizen and a function is also an object. We can assign functions to variables.
Def make_cofe (type): print ("get a cup: {}" .format (type)) get_cofe = make_cofeget_cofe ("coffee") # output # get a cup of coffee
In this example, we assign the function make_cofe to the variable get_cofe, so that when you call get_cofe, it is equivalent to calling the function make_cofe ().
2. Pass the function as a parameter to another function.
Def make_cofe (type): print ("get a cup: {}" .format (type)) def shop (func,type): func (type) shop (make_cofe, "coffee") # output # get a cup of coffee
For this example, we pass make_cofe into shop as an argument, and then call it.
3. Functions can be nested.
Def shop (type): def make_cofe (type): print ("get a cup: {}" .format (type)) make_cofe (type) shop ("coffee") # output # get a cup of coffee
In this code, we define the function make_cofe 4 inside the function shop, and the return value of the function can also be a function object (closure).
Def shop (): def make_cofe (type): print ("get a cup: {}" .format (type)) return make_cofeget_cofe=shop () get_cofe ("coffee") # output # get a cup of coffee
Here, the return value of the function shop () is the function object make_cofe itself, which we then assign to the variable get_cofe and then call get_cofe ("coffee").
Second, decorator
Let's officially start the study of decorators. Let's think about one question first. If we go to the coffee shop to ask for a cup of coffee, how should we achieve it? You might write that.
Def cofe (): print ("coffee", end= "") cofe () # output # coffee
So now we want to have a cup of coffee with sugar. How should we write it? You might think so, but it's not easy. Just change it in the cofe () function.
Def cofe (): print ("coffee with sugar", end= "") cofe () # output # coffee with sugar
So the question is, what if we don't want to drink sweetened coffee now? we can't get rid of it in the cofe () function. So if someone wants to drink coffee with sugar and someone doesn't want to drink coffee with sugar, you can't write two cofe () functions. So let's take a look at the following code with questions.
Def add_sugar (func): def add (): print ("added sugar", end= ") func () return adddef cofe (): print (" coffee ", end=") cofe = add_sugar (cofe) print ("get a cup", end= ") cofe () # output # get a cup of sweetened coffee
The variable cofe points to the internal function add (), and the internal function add () calls the original function cofe (), so when you finally call cofe (), you print 'sugar' and then output 'coffee'. The function add_sugar () here is a decorator that wraps the function cofe () that really needs to be executed and changes its behavior, but the original function cofe () remains the same. Let's take a look at the more elegant way to write it.
Def add_sugar (func): def add (): print ("sweetened", end= ") func () return add@add_sugardef cofe (): print (" coffee ", end=") print ("get a cup", end= ") cofe () # output # get a cup of sweetened coffee
The @ here is called syntactic sugar, and @ add_sugar is equivalent to the previous cofe = add_sugar (cofe) statement, but it is more concise. Therefore, it is recommended to use this method in the program. All right, let's review our question. What if someone wants to drink coffee with sugar and someone doesn't want to drink coffee with sugar? It is very easy to learn the decorator. If we want to drink coffee with sugar, we should add the decorator @ add_sugar with sugar. If we drink without sugar, we will not add the decorator, so we can solve the problem perfectly. Without changing the internal premise of the function, new functions are added to the function. So far, we have learned the simplest decorators. Now we are considering a question: what if there are parameters in the original function cofe () that need to be passed to the decorator? A simple way is to add parameters to the corresponding decorator function add ().
Def add_sugar (func): def add (type): print ("sweetened", end= ") func (type) return add@add_sugardef cofe (type): print (" {} coffee ".format (type), end=") cofe ("American") print () cofe ("latte") # output # Sugar American coffee with sugar latte
But there's a new problem. If I have another function (the milk tea function), I also need to use the add_sugar () decorator, but what if this new function takes two arguments? Normally, we take * args and * * kwargs as arguments to the decorator's internal function add (). * args and * * kwargs, which means to accept any number and type of parameters, so the sugar decorator can be written as follows:
Def add_sugar (func): def add (* args, * * kwargs): print ("added sugar", * * kwargs ") func (* args, * * kwargs) return add @ add_sugardef cofe (type): print (" {} coffee ".format (type), end=") @ add_sugardef milk_tea (type,num): print ("{} cup {} milk tea" .format (num,type)) End= ") cofe (" American ") print () milk_tea (" xx brand "," 4 ") # output # Sugar American coffee with sugar 4 cups of xx brand milk tea
So we can add sugar to our coffee and milk tea. What we talked about earlier is the function decorator, so let's talk about the class as the decorator. The class decorator relies mainly on the function _ _ call__ (), which is executed every time you call an instance of a class.
Class Add_sugar: def _ init__ (self, func): self.func = func self.add_suger = "sweetened" def _ call__ (self, * args, * * kwargs): print (self.add_suger,end= "") return self.func (* args, * * kwargs) @ Add_sugardef cofe (): print ("coffee") cofe () # output # Coffee with sugar
Finally, if we want to add both sugar and ice to our coffee, what should we do? Let's just define a decorator with ice.
Def add_sugar (func): def add (): print ("sweetened", end= ") func () return adddef add_ice (func): def add (): print (" iced ", end=") func () return add@add_sugar@add_icedef cofe (): print ("coffee", end= ") cofe () # output # read here. This "Python decorator example application analysis" article has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about the article, please 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.