In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to realize Python decorator". In daily operation, I believe many people have doubts about how to realize Python decorator. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "how to realize Python decorator"! Next, please follow the small series to learn together!
It says press button 1 to deposit, otherwise withdraw.
No, you have to enter the password to deposit and withdraw money!!! So, we're going to add a password, an authentication code.
It can be seen that although the password verification function is implemented, the code redundancy is relatively high, and now only the withdrawal and deposit functions are simulated, but there are query functions, transfer functions, etc., so the redundancy is even higher, and relative to the withdrawal and deposit functions, the reusability is not so high, so we have to further optimize the code, and write the verification function into the withdrawal and deposit functions.
Is there any way to extend the functionality of the original function without changing the original function and its calls? Of course there is, this is the famous decorator in python. Or use the code above to demonstrate
From now on, the code in the red box above will not be changed, and we will not change it even if we kill it. We will realize the deposit and withdrawal function with password verification.
Decorators are decorated with parametric functions
Look at the code below.
Do you see any problems with this code?
operation results
The inner() function has an extra parameter. One thing to note is that the decorator function returns inner, which means myprint is now equivalent to inner.
Let's change the code.
To enable decorators to decorate more functions, we further optimize the code.
Decorator decorated with return value function
As you can see, whether the decorated function has a return value or not, the result has no return value, and the reason is very simple, because the inner() function has no return value at all. In order for a decorated function to still have a return value, the inner function needs to match the return value of the decorated function.
As you can see, functions with return values are decorated and still have return values, and functions with no return values are decorated and have no return values, which is consistent with our desired results.
double grammatical sugar
To illustrate the double syntax sugar more intuitively, I will not use the above example.
Let me define a function.
Now I have a requirement, I want to print a line '=' and a line '*' above 'python white alliance', which is to achieve this effect.
So how should I write it? You may think of the superposition of grammatical sugar, so which of the following is the superposition method?
Let's run it and we'll see.
It can be seen that the superposition method should be ***, that is to say, the outermost grammar sugar is executed first.
Syntax sugar with parameters
To demonstrate syntactic sugar with parameters, I'm going to bring down one of the pictures above.
Obviously, the red-boxed code is highly redundant, and we can simplify it by using syntax sugar with parameters.
At this point, the study of "how to implement Python decorators" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
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.