Python:为什么 ("hello" is "hello") 评估为 True?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1392433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: Why does ("hello" is "hello") evaluate as True?
提问by Deniz Dogan
Why does "hello" is "hello"
produce True
in Python?
为什么在 Python中"hello" is "hello"
产生True
?
I read the following here:
我在这里阅读了以下内容:
If two string literals are equal, they have been put to same memory location. A string is an immutable entity. No harm can be done.
如果两个字符串文字相等,则它们已被放置到相同的内存位置。字符串是不可变的实体。不能造成任何伤害。
So there is one and only one place in memory for every Python string? Sounds pretty strange. What's going on here?
那么每个 Python 字符串在内存中只有一个地方吗?听起来很奇怪。这里发生了什么?
回答by carl
Python (like Java, C, C++, .NET) uses string pooling / interning. The interpreter realises that "hello" is the same as "hello", so it optimizes and uses the same location in memory.
Python(如 Java、C、C++、.NET)使用字符串池化/实习。解释器意识到“hello”和“hello”是一样的,所以它优化并使用内存中的相同位置。
Another goodie: "hell" + "o" is "hello"
==> True
另一个好东西:"hell" + "o" is "hello"
==>True
回答by bobince
So there is one and only one place in memory for every Python string?
那么每个 Python 字符串在内存中只有一个地方吗?
No, only ones the interpreter has decided to optimise, which is a decision based on a policy that isn't part of the language specification and which may change in different CPython versions.
不,只有解释器决定优化的,这是基于不属于语言规范的一部分的策略的决定,并且可能会在不同的 CPython 版本中发生变化。
eg. on my install (2.6.2 Linux):
例如。在我的安装(2.6.2 Linux)上:
>>> 'X'*10 is 'X'*10
True
>>> 'X'*30 is 'X'*30
False
similarly for ints:
类似的整数:
>>> 2**8 is 2**8
True
>>> 2**9 is 2**9
False
So don't rely on 'string' is 'string': even just looking at the C implementation it isn't safe.
因此,不要依赖“字符串”就是“字符串”:即使只是查看 C 实现也不安全。
回答by Quantumplation
Literal strings are probably grouped based on their hash or something similar. Two of the same literal strings will be stored in the same memory, and any references both refer to that.
文字字符串可能根据它们的散列或类似的东西分组。两个相同的文字字符串将存储在同一个内存中,任何引用都引用它。
Memory Code
-------
| myLine = "hello"
| /
|hello <
| \
| myLine = "hello"
-------
回答by SingleNegationElimination
The is
operator returns true if both arguments are the same object. Your result is a consequence of this, and the quoted bit.
该is
运营商如果两个参数是相同的对象返回true。你的结果是这个结果,以及引用的位。
In the case of string literals, these are interned, meaning they are compared to known strings. If an identical string is already known, the literal takes that value, instead of an alternative one. Thus, they become the same object, and the expression is true.
在字符串文字的情况下,这些是实习的,这意味着它们与已知字符串进行比较。如果已知一个相同的字符串,则文字采用该值,而不是替代值。因此,它们成为同一个对象,表达式为真。
回答by unwind
The Python interpreter/compiler parses the string literals, i.e. the quoted list of characters. When it does this, it can detect "I've seen this string before", and use the same representation as last time. It can do this since it knows that strings defined in this way cannot be changed.
Python 解释器/编译器解析字符串文字,即带引号的字符列表。当它这样做时,它可以检测到“我以前见过这个字符串”,并使用与上次相同的表示。它可以这样做,因为它知道以这种方式定义的字符串不能更改。
回答by Brian Rasmussen
Why is it strange. If the string is immutable it makes a lot of sense to only store it once. .NET has the same behavior.
为什么奇怪。如果字符串是不可变的,那么只存储一次就很有意义。.NET 具有相同的行为。
回答by Brian Rasmussen
I think if any two variables (not just strings) contain the same value, the value will be stored only once not twice and both the variables will point to the same location. This saves memory.
我认为如果任何两个变量(不仅仅是字符串)包含相同的值,该值将只存储一次而不是两次,并且两个变量将指向相同的位置。这样可以节省内存。