对带有字符串的 `is` 运算符感到困惑 [英] Confused about `is` operator with strings
问题描述
is
运算符比较两个对象的内存地址,如果相同则返回 True
.那么,为什么它不能与字符串一起可靠地工作?代码#1
代码#2
<预><代码>>>>ktr = "今天天气很好">>>ptr = "今天天气很好">>>ktr 是 ptr错误的我创建了两个内容相同的字符串,但它们位于不同的内存地址.为什么is
操作符的输出不一致?
我相信这与 string 有关实习.本质上,这个想法是只存储每个不同字符串的一个副本,以提高某些操作的性能.
基本上,a is b
起作用的原因是(正如您可能已经猜到的)在这两种情况下 Python 都引用了一个不可变的字符串.当字符串很大时(以及一些我不明白的其他因素,很可能),这还没有完成,这就是您的第二个示例返回 False 的原因.
事实上,奇怪的行为似乎是交互环境的副作用.如果将相同的代码放入 Python 脚本中,a is b
和 ktr is ptr
都返回 True.
a="poi"b=poi"打印 a 是 b # 打印 'True'ktr = "今天天气很好"ptr = "今天天气很好"打印 ktr 是 ptr # 打印 'True'
这是有道理的,因为 Python 很容易解析源文件并在其中查找重复的字符串文字.如果您动态创建字符串,那么即使在脚本中它的行为也会有所不同.
a="p" + "oi"b="po" + "i"print a is b # 奇怪的是,打印 'True'ktr = "今天是" + " 美好的一天"ptr = "今天是 f" + "ine day"打印 ktr 是 ptr # 打印 'False'
至于为什么 a is b
的结果仍然是 True,也许分配的字符串足够小以保证快速搜索内部集合,而另一个不是?
The is
operator compares the memory addresses of two objects, and returns True
if they're the same. Why, then, does it not work reliably with strings?
Code #1
>>> a = "poi"
>>> b = "poi"
>>> a is b
True
Code #2
>>> ktr = "today is a fine day"
>>> ptr = "today is a fine day"
>>> ktr is ptr
False
I have created two strings whose content is the same but they are living on different memory addresses. Why is the output of the is
operator not consistent?
I believe it has to do with string interning. In essence, the idea is to store only a single copy of each distinct string, to increase performance on some operations.
Basically, the reason why a is b
works is because (as you may have guessed) there is a single immutable string that is referenced by Python in both cases. When a string is large (and some other factors that I don't understand, most likely), this isn't done, which is why your second example returns False.
EDIT: And in fact, the odd behavior seems to be a side-effect of the interactive environment. If you take your same code and place it into a Python script, both a is b
and ktr is ptr
return True.
a="poi"
b="poi"
print a is b # Prints 'True'
ktr = "today is a fine day"
ptr = "today is a fine day"
print ktr is ptr # Prints 'True'
This makes sense, since it'd be easy for Python to parse a source file and look for duplicate string literals within it. If you create the strings dynamically, then it behaves differently even in a script.
a="p" + "oi"
b="po" + "i"
print a is b # Oddly enough, prints 'True'
ktr = "today is" + " a fine day"
ptr = "today is a f" + "ine day"
print ktr is ptr # Prints 'False'
As for why a is b
still results in True, perhaps the allocated string is small enough to warrant a quick search through the interned collection, whereas the other one is not?
这篇关于对带有字符串的 `is` 运算符感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!