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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what is the residence of python strings". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Let's take a look at the following code
> a = 'apple'
> b = 'apple'
> an is b
True
The return result looks fine.
Let's take a look at this paragraph:
> a = 'applegoat'
> b = 'applegoat'
> an is b
False
Isn't that a little weird?
The difference between the two strings is only one word, and the result is completely different.
If you don't believe me, you can test it yourself.
In order to understand the difference between this code, there is a concept called string resident, which is called string intern in English.
What is string resident
Everything is an object in python, and in some cases, the interpreter will make some optimizations when it encounters two objects that look the same.
For example, in the first piece of code in the above example, the values of an and b are both apple. When the interpreter is ready to create the second string "apple", the interpreter will go to resident memory to find out whether a "apple" object already exists. If so, it will no longer create a new object. The two share a wife, oh no, share an object.
This is the concept of string residency.
So the two variables an and b share the same object, so the result returned after the is operation is True.
For simple strings, there may be a lot of repetition in the system, which has the advantage of saving memory. The policy of hosting memory also exists in java.
But not all strings will be placed in resident memory, if they all reside, memory will explode. It is possible to be resident only if the following rules are met.
A string of length 0 or 1 is resided.
The string will reside when it contains only letters, numbers or underscores.
The string must be a constant string at compile time
For example, the string generated by the following c using the join method does not reside
> a = "apple"
> c = "" .join (['axie'])
> an is c
This is the end of the content of False "what is the host of python strings". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.