> 文章列表 > 【Python】Python中神奇的字符串驻留机制

【Python】Python中神奇的字符串驻留机制

【Python】Python中神奇的字符串驻留机制

今天有一个初学者在学习Python的时候又整不会了。 原因是以下代码

a = [1, 2, 3]  
b = [1, 2, 3]  
if a is b:  print("a and b point to the same object")  
else:  print("a and b point to different objects")

运行结果是a and b point to different objects

然后他又试了字符串对象。

str1 = "hello"  
str2 = "hello"  if str1 is str2:  print("str1 and str2 are the same object")  
else:  print("str1 and str2 are different objects")

运行结果是:str1 and str2 are the same object

他找到田辛老师询问原因。 于是有了今天这篇文章:Python中的字符串驻留机制

1 什么是字符串驻留机制?

字符串驻留机制是Python针对字符串对象采取的一种内存优化技术。其目标是减少内存使用并提高程序的性能。

在Python中,字符串是不可变的对象,一旦创建,就不能被修改。因此,如果我们创建了两个字符串,它们将占用两个不同的内存位置。但是,如果这些字符串是相同的,那么它们将被Python自动合并为一个对象,这就是字符串驻留机制。

Python中的字符串驻留机制是通过使用intern机制来实现的。当我们创建一个字符串时,Python会检查字符串池中是否已经存在相同的字符串。如果是,它将返回现有的字符串对象的引用,而不是创建一个新的对象。这样,我们可以在不占用额外内存的情况下使用相同的字符串。

2 如何使用字符串驻留机制?

Python的字符串驻留机制在不同版本之间有一些变化。下面是一些主要的变化:

  1. Python 2.x和Python 3.x的字符串驻留机制不同。在Python 2.x中,只有长度为1的字符串才会被驻留。而在Python 3.x中,长度为0到20的字符串都会被驻留。 (更长的田辛老师试过,似乎都可以)
  2. 在Python 3.7之前,字符串驻留机制只适用于ASCII字符集中的字符串。这意味着只有ASCII字符集中的字符串才会被驻留。但是,在Python 3.7中,这个限制已经被取消,所有字符串都可以被驻留。
  3. 在Python 3.8中,字符串驻留机制的实现发生了变化。在之前的版本中,字符串池是一个全局的数据结构,所有的字符串都存储在其中。但是,在Python 3.8中,字符串池被改为了一个线程局部的数据结构,每个线程都有自己的字符串池。这样可以提高多线程程序的性能。

需要注意的是,字符串驻留机制是Python的一种优化技术,它并不是Python语言规范的一部分。因此,不同的Python实现(如CPython、Jython、IronPython等)可能会有不同的字符串驻留机制实现。

正是因为上面的变化, 所以有的时候田辛老师觉得字符串驻留在面向内存的时候是友好的, 但确实会给新手带来一些不便。

作为新手, 田辛老师提供一个手动使用字符串驻留的例子:

import sys # 使用sys.intern()函数启用字符串驻留机制 
str3 = sys.intern("hello") 
str4 = sys.intern("hello") # 使用is关键字比较两个字符串是否相同 
if str3 is str4: print("str3 and str4 are the same object") 
else: print("str3 and str4 are different objects")

上面的例子中,田辛老师使用sys.intern()函数将字符串“hello”添加到字符串池中,并将返回的引用分配给str3和str4。然后,我们再次使用is关键字比较它们是否相同。由于它们是相同的对象,因此输出结果为“str3 and str4 are the same object”。

3 简单拼接驻留, 运行时不驻留

我们来看下面的代码:

str5 = 'tianxin' + 'training'  
print(str5 is 'tianxintraining')  str6 = 'tianxin'  
str7 = 'training'  
print((str6 + str7) is 'tianxintraining')

大家可以猜一猜输出结果是什么? 是两个true,还是一个true一个false?

答案是:

True
False

为什么会出现这种情况呢? 第一个打印,是两个字符串简单拼接, 那么就会实现字符串驻留。 但是, 一旦是变量拼接字符串机制就不能用了。
关于这一点, 田辛老师通过如下代码带领大家查看一下上述两个处理的汇编执行过程:

import dis  def pro1():  str5 = 'tianxin' + 'training'  print(str5 is 'tianxintraining')  def pro2():  str6 = 'tianxin'  str7 = 'training'  print((str6 + str7) is 'tianxintraining')  print('================================================================')  
dis.dis(pro1)  
print('================================================================')  
dis.dis(pro2)

输出结果:

================================================================20           0 LOAD_CONST               1 ('tianxintraining')2 STORE_FAST               0 (str5)21           4 LOAD_GLOBAL              0 (print)6 LOAD_FAST                0 (str5)8 LOAD_CONST               1 ('tianxintraining')10 IS_OP                    012 CALL_FUNCTION            114 POP_TOP16 LOAD_CONST               0 (None)18 RETURN_VALUE
================================================================25           0 LOAD_CONST               1 ('tianxin')2 STORE_FAST               0 (str6)26           4 LOAD_CONST               2 ('training')6 STORE_FAST               1 (str7)27           8 LOAD_GLOBAL              0 (print)10 LOAD_FAST                0 (str6)12 LOAD_FAST                1 (str7)14 BINARY_ADD16 LOAD_CONST               3 ('tianxintraining')18 IS_OP                    020 CALL_FUNCTION            122 POP_TOP24 LOAD_CONST               0 (None)26 RETURN_VALUE进程已结束,退出代码0

我们可以看到, Python的解释器对这种简单的字符串拼接在形成字符码的时候,就已经进行了拼接。 str5 你写不写成拼接的形式都是一样的。

4 总结

Python的字符串驻留机制是一种优化技术,它可以减少内存使用并提高程序的性能。它的做法的基础是Python字符串对象的不可改变的特性。 当然, 不同的Python版本对于字符串驻留机制的处理不同可能会给初学者带来一些麻烦。 Python学习者必须要面对的一个问题。

5 全部代码

上面已经有了关于输出汇编的全部代码, 就不再重新输出了。 只展示字符串驻留部分的代码:

#!/usr/bin/env python  
# -*- coding:utf-8 -*-  
"""  
#-----------------------------------------------------------------------------  
#                     --- TDOUYA STUDIOS ---  
#-----------------------------------------------------------------------------  
#  
# @Project : di08-tdd-cdg-python-learning  
# @File    : str_intern.py  
# @Author  : tianxin.xp@gmail.com  
# @Date    : 2023/4/5 10:34  
#  
# 代码说明  
#  
#--------------------------------------------------------------------------"""  
import sys  a = [1, 2, 3]  
b = [1, 2, 3]  
if a is b:  print("a and b point to the same object")  
else:  print("a and b point to different objects")  str1 = "hello"  
str2 = "hello"  if str1 is str2:  print("str1 and str2 are the same object")  
else:  print("str1 and str2 are different objects")  # 使用sys.intern()函数启用字符串驻留机制  
str3 = sys.intern("hello")  
str4 = sys.intern("hello")  # 使用is关键字比较两个字符串是否相同  
if str3 is str4:  print("str3 and str4 are the same object")  
else:  print("str3 and str4 are different objects")  str5 = 'tianxin' + 'training'  
print(str5 is 'tianxintraining')  str6 = 'tianxin'  
str7 = 'training'  
print((str6 + str7) is 'tianxintraining')

执行结果:警告可忽略

D:\\python-grp\\miniconda_env\\py3.10\\python.exe E:\\develop\\python\\di08-tdd-cdg-python-learning\\src\\std_str_intern\\str_intern.py 
a and b point to different objects
str1 and str2 are the same object
str3 and str4 are the same object
True
False
E:\\develop\\python\\di08-tdd-cdg-python-learning\\src\\std_str_intern\\str_intern.py:44: SyntaxWarning: "is" with a literal. Did you mean "=="?print(str5 is 'tianxintraining')
E:\\develop\\python\\di08-tdd-cdg-python-learning\\src\\std_str_intern\\str_intern.py:48: SyntaxWarning: "is" with a literal. Did you mean "=="?print((str6 + str7) is 'tianxintraining')进程已结束,退出代码0