In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What are the characteristics of the methods that provide us with solutions in Python? I believe many inexperienced people are at a loss about this. Therefore, this article summarizes the causes and solutions of the problems. Through this article, I hope you can solve this problem.
In fact, in our daily work, many of our requirements, whether common or unusual, Python provides us with some unique solutions that do not need to build our own wheels or introduce new dependencies (introducing new dependencies is bound to increase the complexity of the project).
But there are so many features and features of Python that we can't make a good decision in the first place when we encounter problems.
So, let's clean up these Python dead corners that we have ignored.
The wonderful use of decorators
We often want to complete some registration-calling functions, such as we have four functions:
Now we want to bind these four functions to the +, -, *, / four operators, so what should we do?
Maybe our first reaction is like this:
But when it comes to writing in this way, there is a big problem is that it is too ugly. Since the maintainability of direct dict operations is actually very poor, what should we do in this place?
Before improving this code, we first need to clarify a very important concept in Python, that is, the function / method is: First Class Member. In imprecise terms, functions / methods can be passed and used as parameters.
For example:
You can see that we passed the print_func function as an argument to the execute function and was called.
So let's modify the previous code:
All right, let's see, has the readability and maintainability of the overall code changed a lot?
But our problem now is that we need to call the register_operator function separately every time, which is so annoying! Do you want to improve it? I want it. We can improve it with decorators.
First, take a look at the simplest example of a decorator:
We can see that the meaning of this function is to calculate the execution time of the function. So what is this principle?
The decorator is actually a grammatical sugar, which can be found in PEP318 Decorators for Functions and Methods.
In short, it was actually Python who did a replacement process for us. In the above example, the replacement process is add=execute (add).
All right, let's use this knowledge to improve the previous code:
Does this make the registration process of our code more elegant?
Well, yes! In fact, there are many features in Python that will help our code be simpler and more beautiful.
The next example is likely to help us reduce our workload.
Talk about OrderedDict.
Dict is a kind of data deconstruction that we often use. But before Python 3.6the dict was disordered, that is, the order in which I inserted it had nothing to do with the order in which the data was stored in the dict. (the author's note: Python 3.6dict ordering is only a byproduct of the new version, and Python 3.7is officially fixed as feature).
But in many cases, such as in scenarios such as visa verification, we need to ensure that the dict data is stored in the same order as our insertion order. So what do we do?
The boss has a demand, and we certainly can't tell the boss that we can't do it. Then let's implement an ordereddict by ourselves. So, after thinking about it, I wrote the following code:
Maintain the order of key inserts by maintaining an additional list. This code seems to complete our requirements, but there is actually a big problem. Can you guess what the problem is?
3,2,1!
To reveal the answer, this code uses list to ensure the order of key. When deleting, the delete operation of list is a time complexity O (n) operation. In other words, the more internal data we delete, the longer it takes to delete. This is unacceptable for some performance-sensitive scenarios.
What are we going to do? In fact, Python has built in an ordered dictionary for a long time, that is, collections.OrderedDict, which many people may have used.
In OrderedDict, Python maintains a bi-directional list deconstruction to ensure the ordering of inserts, as shown in the following figure:
Maintain a guard node on the far left, with the guard node's next pointer always pointing to the last inserted node in the data. Then when inserting new data, we insert the new data behind the guard node, so as to achieve the purpose of maintaining the insertion order.
At the time of deletion, the node corresponding to the key to be deleted is found through an additional maintained dictionary. This operation is the complexity of O (1), and as we all know, the time complexity of deleting a node from a two-way linked list is also O (1). In this way, we can ensure the corresponding performance even if we have a large amount of data.
All right, let's follow this idea to do the simplest implementation:
This is just a simplified version of OrderedDict, and there are still a lot of corner case to deal with if you want to complete a complete OrderedDict. For now, however, we can use built-in data structures to fulfill our requirements. So, do you have a feeling of happiness?
Have a casual chat
Through two examples today, we find that Python provides quite a lot of functions to help us complete our daily work and study tasks. At the same time, through in-depth understanding of some of the internal functions of Python, so that we can better learn some knowledge.
After reading the above, have you mastered the method features in Python that provide us with solutions? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.