What is string interning (string dwell) and the intern mechanism of strings in Python

Source: Internet
Author: User

incomputer Science, string interning are a method of storing only onecopy of each distinct string value, which must be Immutable . interning strings makes some stringprocessing tasks more time-or space-efficient at the cost of requiring moretime when t He string is created or interned. The distinct values are stored ina string intern pool. -- quote from Wikipedia

In other words, a string object with the same value will only hold one copy, which is common, which also determines that the string must be an immutable object. Think about it, just like a numeric type, the same value can only be saved in one copy, and there is no need to differentiate it with different objects.


python the string in the Intern mechanism, will automatically Intern .

>>a = ' Kzc '
>>b = ' k ' + ' ZC '
>>id (a)
55704656
>>id (b)
55704656

As you can see, they are the same object.


Intern the advantage of the mechanism is that it takes the same string (such as an identifier) to use it directly from the pool, avoiding frequent creation and destruction, increasing efficiency and saving memory. The disadvantage is that it affects performance by stitching strings, modifying strings, and so on. Because it is immutable, it is not a inplace operation to modify a string , to create a new object. This is why it is not recommended to use the + and join ()when stitching multiple strings. Join () calculates the length of all the strings, then copies them all, and then only new objects.

You need to be careful of the pits , not all strings will use the intern mechanism. Strings that contain only underscores, numbers, and letters are intern.

>>a = ' Hello World '
>>b = ' Hello World '
>>id (a)
56400384
>>id (b)
56398336

Because there are spaces here, all are not intern.

But why did you do it? Since the Python built-in function intern () can explicitly intern any string. The description is not a problem to achieve difficulty.

The answer in the source stringobject.h can be found in the comments,

/* ... ... This was generally restricted tostrings that "looklike" Python identifiers, although the Intern () Builtincan be US Ed to force interning of any string ... * *

In other words, only those that look like Python identifiers are intern.

Look at the other hole below,

Example 1.

>> ' KZ ' + ' C ' is ' KZC '
True

Example 2.
>>s1 = ' KZ '
>>s2 = ' Kzc '
>>s1+ ' C ' is ' KZC '
False

Why the second chestnut is false, contains only the letter Ah, should not be automatically intern?

This is because the first pest, ' KZ ' + ' C ' is evaluated at compile time and replaced by ' Kzc '.

While the second chestnut, s1+ ' C ' is stitched in run-time, resulting in not being automatically intern.


What is string interning (string dwell) and the intern mechanism of strings in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.