The premise of Python string pooling

Foreword

In Python, memory pooling technology is often used to improve its performance, so the question is, under what circumstances will pooling? Let us understand through a few examples.

Preliminary knowledge

Before looking at the example, let us first mention a function id() in python, let us look at the function description:

id(obj,/)
 Return the identity of an object.

 This is guaranteed to be unique among simultaneously existing objects.(CPython uses the object \'s memory address.)

Through the above description, we can know that id() will return the unique identifier of the object, and in CPython will return the memory address, that is to say, if the id value of two objects is the same, it can be said that the two objects are the same. .

example

example 00

a =""
b =""print(id(a),id(b))print(a is b)

Output result:

a = “”
b = “”
print(id(a),id(b))
2114853370544 2114853370544
print(a is b)
True

example 01

a ="a"
b ="a"print(id(a),id(b))print(a is b)

Output result:

a = “a”
b = “a”
print(id(a),id(b))
2114883022608 2114883022608
print(a is b)
True

example 02

a ="magic_string"
b ="magic"+"_"+"string"print(id(a),id(b))print(a is b)

Output result:

a = “magic_string”
b = “magic” + “_” + “string”
print(id(a),id(b))
2114887161136 2114887161136
print(a is b)
True

example 03

a ="magic!"
b ="mgaic!"print(id(a),id(b))print(a is b)

Output result:

a = “magic!”
b = “mgaic!”
print(id(a),id(b))
2114885855416 2114889455408
print(a is b)
False

example 04

a,b ="magic!","magic!"print(id(a),id(b))print(a is b)

Output result:
a,b = “magic!”,”magic!”
print(id(a),id(b))
2114885691912 2114885691912
print(a is b)
True

example 05

a ="!"
b ="!"print(id(a),id(b))print(a is b)

Output result:

a = “!”
b = “!”
print(id(a),id(b))
140564571922024 140564571922024
print(a is b)
True

example 06

print(a*20 is 'aaaaaaaaaaaaaaaaaaaa')print(a*21 is 'aaaaaaaaaaaaaaaaaaaaa')

Output result:

print(a20 is ‘aaaaaaaaaaaaaaaaaaaa’)
False
print(a
21 is ‘aaaaaaaaaaaaaaaaaaaaa’)
False

to sum up

Through the above 7 examples, it is not difficult for us to have a general understanding of python string pooling. Let's make a brief summary here:

  1. Through example 00, 01, 05, we can conclude that strings with a length of 0 or 1 will be pooled
  2. Through example 02, 03, we can conclude that strings containing only alphanumeric characters and underscores will be pooled
  3. Through example 04, we can get that when assigning different variables on the same line, if they are assigned the same value, they will point to the same object. Note that the "magic!" inside does not meet the requirements of pooling. This is just a compilation Optimization
  4. The phenomenon of example 06 has a professional term in python. Constant folding. As the name implies, when compiling and optimizing, the variables that can be calculated are directly replaced with constants. But is there no restriction? Obviously not. In our example, we have found that when the length exceeds 20, the folding will fail. Just imagine, if there is no limit, the initialized string is too long, which will seriously cause performance degradation and memory Consume

Reference link

The internals of Python string interning

exploring python code objects

Python string interning

Python String objects implementation

The above is the detailed content of the premise of Python string pooling. For more information about Python string pooling, please pay attention to other related articles on ZaLou.Cn!

Recommended Posts

The premise of Python string pooling
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
Consolidate the foundation of Python(6)
Consolidate the foundation of Python(5)
Consolidate the foundation of Python (3)
The usage of wheel in python
Python string
Python handles the 4 wheels of Chinese
Python simulation of the landlord deal
What is the use of Python
Secrets of the new features of Python 3.8
Python string
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
End the method of running python
Understanding the meaning of rb in python
Can Python implement the structure of the stack?
Learn the basics of python interactive mode
What are the required parameters of python
Logistic regression at the bottom of python
The usage of Ajax in Python3 crawler
Python solves the Tower of Hanoi game
Python string to judge the password strength
Solve the conflict of multiple versions of python
What is the scope of python variables
Python implements the sum of fractional sequences
Two days of learning the basics of Python
What is the id function of python
Where is the pip path of python3
The essence of Python language: Itertools library
What are the advantages of python language
The specific method of python instantiation object
python3 realizes the function of mask drawing
What is the function body of python
The specific method of python import library
Solve the conflict of multiple versions of python
Python string manipulation
7 features of Python3.9
2.1 The Python Interpreter (python interpreter)
What is the function of adb in python
Detailed explanation of the principle of Python super() method
The difference between the syntax of java and python
Python implements the shuffling of the cards in Doudizhu
The meaning and usage of lists in python
Solve the problem of python running startup error
Python implements the source code of the snake game
How about learning python at the age of 27?
The consequences of uninstalling python in ubuntu, very
Python writes the game implementation of fishing master
[898] python get the intersection of two lists | union | difference
Detailed explanation of the principle of Python function parameter classification
What is the advantage of python over corporate language
A brief summary of the difference between Python2 and Python3
Detailed explanation of the principle of Python timer thread pool
Solve the problem of python compiling and installing ssl
Detailed explanation of the implementation steps of Python interface development
How does python call the key of a dictionary
Python crawls the full set of skins of the king pesticide
How to understand the introduction of packages in Python