Python中字符串的intern机制
intern机制:
字符串类型作为Python中最常用的数据类型之一,Python解释器为了提高字符串使用的效率和使用性能,做了很多优化,例如:Python解释器中使用了 intern(字符串驻留)的技术来提高字符串效率,什么是intern机制?即值同样的字符串对象仅仅会保存一份,放在一个字符串储蓄池中,是共用的,当然,肯定不能改变,这也决定了字符串必须是不可变对象。
简单原理:
实现 Intern 机制的方式非常简单,就是通过维护一个字符串储蓄池,这个池子是一个字典结构,如果字符串已经存在于池子中就不再去创建新的字符串,直接返回之前创建好的字符串对象,如果之前还没有加入到该池子中,则先构造一个字符串对象,并把这个对象加入到池子中去,方便下一次获取。
但是,解释器内部对intern 机制的使用策略是有考究的,有些场景会自动使用intern ,有些地方需要通过手动方式才能启动,看下面几个常见的小陷阱。
1.在shell中示例,并非全部的字符串都会采用intern机制。仅仅包括下划线、数字、字母的字符串才会被intern,当然不能超过20个字符。因为如果超过20个字符的话,解释器认为这个字符串不常用,不用放入字符串池中。
>>> s1="hello"
>>> s2="hello"
>>> s1 is s2
True
# 如果有空格,默认不启用intern机制
>>> s1="hell o"
>>> s2="hell o"
>>> s1 is s2
False
# 如果一个字符串长度超过20个字符,不启动intern机制
>>> s1 = "a" * 20
>>> s2 = "a" * 20
>>> s1 is s2
True
>>> s1 = "a" * 21
>>> s2 = "a" * 21
>>> s1 is s2
False
>>> s1 = "ab" * 10
>>> s2 = "ab" * 10
>>> s1 is s2
True
>>> s1 = "ab" * 11
>>> s2 = "ab" * 11
>>> s1 is s2
False
2.但是在pycharm中,只要是同一个字符串不超过20个字符,都为True,并不用是下划线、数字、字母的字符串。个人理解:IDE支持的不好。
s1 = "hell o"
s2 = "hell o"
print(s1 is s2) # True
s1 = "hell!*o"
s2 = "hell!*o"
print(s1 is s2) # True
s1 = "a" * 20
s2 = "a" * 20
print(s1 is s2) # True
s1 = "a" * 21
s2 = "a" * 21
print(s1 is s2) # False
s1 = "ab" * 10
s2 = "ab" * 10
print(s1 is s2) # True
s1 = "ab" * 11
s2 = "ab" * 11
print(s1 is s2) # False
3.字符串拼接时,涉及编译运行问题
>>> s1 = "hell"
>>> s2 = "hello"
>>> s1 + "o" is s2
False
>>> "hell" + "o" is s2
True
>>>
# 说明shell和IDE在这方面没有差异
s1 = "hell"
s2 = "hello"
print(s1 + "o" is s2) # False
print("hell" + "o" is s2) # True
#因为"hell" + "o"在编译时已经变成了"hello",而s1+"o"因为s1是一个变量,他们会在运行时进行拼接,所以没有被intern
Python中字符串的intern机制的更多相关文章
- 什么是string interning(字符串驻留)以及python中字符串的intern机制
Incomputer science, string interning is a method of storing only onecopy of each distinct string val ...
- 区分Python中的id()和is以及Python中字符串的intern机制
参考:1. https://blog.csdn.net/lnotime/article/details/81194633 2.https://segmentfault.com/q/1010000015 ...
- python中字符串的操作方法
python中字符串的操作方法大全 更新时间:2018年06月03日 10:08:51 作者:骏马金龙 我要评论这篇文章主要给大家介绍了关于python中字符串操作方法的相关资料,文中通过示例代码详细 ...
- python中字符串的几种表达方式(用什么方式表示字符串)
说明: 今天在学习python的基础的内容,学习在python中如何操作字符串,在此记录下. 主要是python中字符串的几种表达,表示方式. python的几种表达方式 1 使用单引号扩起来字符串 ...
- Python中字符串String的基本内置函数与过滤字符模块函数的基本用法
Python中字符串String的基本内置函数与用法 首先我们要明白在python中当字符编码为:UTF-8时,中文在字符串中的占位为3个字节,其余字符为一个字节 下面就直接介绍几种python中字符 ...
- Python中字符串与字节之间相互转换
Python中字符串与字节之间相互转换 a = b"Hello, world!" # bytes object b = "Hello, world!" # ...
- Python中字符串的学习
Python中字符串的学习 一.字符串的格式化输出 % 占位符 %s 字符串 %d integer %x 十六进制 integer %f float 指定长度 %5d 右对齐,不足左边补空格 %-5d ...
- python中字符串的四种表达方式
今天在学习python的基础的内容,学习在python中如何操作字符串,在此记录下. 主要是python中字符串的几种表达,表示方式. python的几种表达方式 1 使用单引号扩起来字符串 > ...
- 超详细!盘点Python中字符串的常用操作
在Python中字符串的表达方式有四种 一对单引号 一对双引号 一对三个单引号 一对三个双引号 a = 'abc' b= "abc" c = '''abc''' d = " ...
随机推荐
- 【CUDA学习】__syncthreads的理解
__syncthreads()是cuda的内建函数,用于块内线程通信. __syncthreads() is you garden variety thread barrier. Any thread ...
- Axiom3D写游戏:用Overlay实现Mesh浏览.
从网上找了些资源,大多搜Ogre,Mesh资源,然后为了方便查看各个Mesh,以及对应骨骼动画.为了实用性,考虑放在原游戏窗口里实现.最开始打算窗口新建viewport来实现,后发现这种方式的局限性, ...
- 如何以Java实现网页截图技术
转自 http://blog.csdn.net/cping1982/article/details/5353049 今天看到某网友关于“如何以Java实现网页截图技术”的咨询帖,由于出现该咨询的地 ...
- 根据URL获取参数值得出json结果集,对外给一个接口让别人调用
背景:测试接口的时候,经常都是后端get\post请求直接返回json结果集,很想知道实现方式,虽然心中也大概了解如何实现,但还不如自己来一遍踏实! 先来看一下结果吧: 以下对一个web的get接口进 ...
- 原创:XXX公司-基于SAP的库存管理系统解决方案
XXX公司-基于SAP的库存管理系统 解决方案 版本:V0.3.0 Excel_Cortan 文件状态: [ ] 草稿 [ ] 正式发布 [√] 正在修改 文件标识: 当前版本: V0.3 作 者 ...
- SQLException: Column count doesn't match value count at row 1
INSERT INTO table_name(col_name1, col_name2, col_name3) VALUES('value1','value2'); 语句中,前后列数不等造成的 转自: ...
- idea中maven导入jar包
导入servlet和jsp的jar包 要想在pom.xml中配置一个依赖,必须要知道这个依赖库的坐标:groupId,artifacatId,version 1.可以通过查阅依赖库的资料获取坐标,然后 ...
- spring cloud feign 上传文件报not a type supported by this encoder解决方案
上传文件调用外部服务报错: not a type supported by this encoder 查看SpringFormEncoder类的源码: public class SpringFormE ...
- 【玩转Golang】reflect.DeepEqual
如果有两个map,内容都一样,只有顺序不同 m1:=map[,,}; m2:=map[,,}; 我们怎么判断二者是否一致呢? 如果你打算这么写: fmt.Println("m1==m2&qu ...
- 【python】并行化的又一种思路
https://segmentfault.com/a/1190000000414339