SoFunction
Updated on 2024-12-19

The scope of use of python string residency mechanism knowledge points in detail

1. When the length of the string is 0 and 1.

2. A string that matches the identifier.

3. Strings only reside at compile time, not runtime.

4. Integer numbers between [-5,256].

an actual example

>>> str1='jiumo'
>>> str2='jiumo'
>>> str1 is str2
True
>>> id(str1)
1979078421896
>>> id(str2)
1979078421896

Knowledge point expansion:

timing of stay

All strings of length 0 and length 1 are resident.

Strings are only resident at compile time, not runtime

a = 'hi' # a variable is resident
b = ''.join(['h', 'i']) # The variable will not be resident
print(a is b) # False

principle

The system maintains interned dictionaries to record the string objects that have been resident

When a string object a needs to reside, it first detects whether it exists in the interned, and if it exists, it points to the existing string object, and the reference count of a is reduced by 1.
If it does not exist, record a in interned

To this point this article on the use of python string residency mechanism of the scope of knowledge is introduced to this article, more related to the use of python string residency mechanism of the scope of content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!