In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what is the Python string Intern mechanism". In the daily operation, I believe many people have doubts about what the Python string Intern mechanism is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what is the Python string Intern mechanism?" Next, please follow the editor to study!
String is one of the simplest and most commonly used data types in Python. A technology called Intern (string resident) is used to improve string efficiency in the implementation of strings in CPython. So what exactly is the intern mechanism, and how does it improve string efficiency?
Let's take a look at a piece of code:
> S3 = "hello!"
> S4 = "hello!"
> S3 is S4
False
> > id (S3)
80325968L
> > id (S4)
80326048L
Although S3 and S4 have the same value, they are indeed two different string objects. Python allocates a section of memory space for both of them. Assuming that there are a large number of strings with the same value in the program, the system has to allocate memory space repeatedly for each string. Obviously, it is a waste of resources for the system. To solve this problem, Python introduces the intern mechanism for strings.
Let's take a look at:
> S3 = intern ('hellographies')
> S4 = intern ('hellograms')
> S3 is S4
True
> > id (S3)
80325968L
> > id (S4)
80325968L
Intern is a built-in function in Python. The function is to intern the string and return the string object after processing. We find that all strings with the same value will return the same string object after being processed by the intern mechanism. This way can undoubtedly save more memory space when dealing with big data. There is no need for the system to repeatedly allocate memory for the same string, but to share the same object for the string with the same value.
In fact, the way to implement the Intern mechanism is very simple, that is, by maintaining a string savings pool, which is a dictionary structure, if the string already exists in the pool, it will no longer create a new string, but directly return the previously created string object. If it has not been added to the pool, construct a string object first, and add the object to the pool to facilitate next acquisition. With pseudo code, it can be described as: intern_pool = {}
Def intern (s): if s in intern_pool:
Return intern_ pool[s]
Else: obj = PyStringObject (s) intern_ Pool [s] = obj
Return obj
In the mainstream object-oriented programming languages, the string intern mechanism has become a standard for efficient string processing, and the string processing efficiency can be improved through the intern mechanism. Of course, there is a sophisticated strategy for the use of the intern mechanism in the interpreter, some scenarios will automatically use intern, and some places need to be started manually. For example:
> > S1 = "hello"
> S2 = "hello"
> > S1 is S2
True
> > id (S1)
72320704L
> id (S2)
72320704L
This code is the result of Python's automatic use of the intern mechanism.
At this point, the study on "what is the Python string Intern mechanism" is over. I hope to be able to 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: 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.