In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what is the role of decorators in python". In daily operation, I believe that many people have doubts about the role of decorators in python. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is the role of decorators in python?" Next, please follow the editor to study!
The function of the decorator is to use a new function to encapsulate the old function (adding the function without changing the old function code) and then return a new function. The new function is called the decorator. Generally, in order to simplify, the decorator will use the French sugar @ new function to simplify.
Example:
This is a piece of code, but there are too few features to enhance it, but you can't change the code.
Def hello (): return "hello world!"
Now our requirement is to enhance the functionality of the hello () function and want to tag the return with a HTML tag, such as hello world, but requires us not to change the original definition of the hello () function.
Def makeitalic (fun): # makitalic passes a new function def wrapped (): # Internal function return "" + fun () + "" # the new function to be added return wrapped# returns the wrapped function function def hello (): # to enhance this function, return "hello world!" # makeitalic introduces the hello function, and then the internal function fun () function is equivalent to the hello function hello_2=makeitalic (hello) # print the new function What is returned is the hello worldprint (hello_2 ())
In order to enhance the function of the original function hello, a function is defined, which takes the original function as a parameter and returns a new function. In this returned function, the original function is executed and the function of the original function is enhanced.
In fact, makeitalic is a decorator that encapsulates the original function hello and returns a new function to enhance the functionality of the original function and assign it to hello.
In general, we use @ grammatical sugar (Syntactic Sugar) provided by the decorator to simplify the above operation.
# using @ syntax sugar def makeitalic (fun): def wrapped (): return "" + fun () + "" return wrapped@makeitalic# can be called directly without assigning def hello (): return "hello world" print (hello ()) # can be called directly without assignment.
As in the case above, a function that can dynamically modify the function (or class) function is the decorator. In essence, it is a high-order function that takes the decorated function (such as hello above) as an argument and returns a wrapped function (such as wrapped above) to the decorated function (hello).
When the hello () function is called, the execution flow of the hello function is analyzed as follows:
1. Pass the hello function as an argument to the decorator function after the @ symbol.
two。 Then start executing the decorator function and return a wrapped function, at the same time, change the direction of the original function, which now points to the wrapper function.
3. When the original function is executed, it is actually a wrapped function, so the decorator enhances the functionality of an existing function, but does not change the definition of the existing function.
The use of ordinary decorators:
The @ decoratordef fun (): pass# format looks like the following: def fun (): passfun = decorator (fun) # assign without syntax sugar
Decorators can be defined, and the decorator closest to the function definition is called first, such as:
@ decotator_one@decorator_twodef fun (): pass# format is like the following: def fun (): passfun = decorator_one (decorator_two (fun))
The decorator can also take parameters, such as:
@ decorator (arg1, arg2) def fun (): pass# format is like the following: def fun (): passfun = decorator (arg1, arg2) (fun). At this point, the study on "what is the role of decorators in python" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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: 220
*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.