Python:为什么(“hello”是“hello”)评估为True?

发布于 2024-08-04 07:39:32 字数 330 浏览 12 评论 0原文

为什么在 Python 中 "hello" is "hello" 会产生 True

我在此处阅读了以下内容:

如果两个字符串文字相等,则它们已被置于相同的位置 内存位置。字符串是一个不可变的实体。没有伤害可以 完成。

那么每个 Python 字符串在内存中都只有一个位置吗?听起来很奇怪。这是怎么回事?

Why does "hello" is "hello" produce True in Python?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

遗心遗梦遗幸福 2024-08-11 07:39:32

Python(如 Java、C、C++、.NET)使用字符串池/驻留。解释器意识到“hello”与“hello”相同,因此它优化并使用内存中的相同位置。

另一个好东西:“hell”+“o”是“hello” ==> 正确

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.

Another goodie: "hell" + "o" is "hello" ==> True

人间☆小暴躁 2024-08-11 07:39:32

那么每个 Python 字符串在内存中都只有一个位置吗?

不,只有解释器决定优化的那些,这是基于不属于语言规范的一部分的策略的决定,并且可能在不同的 CPython 版本中发生变化。

例如。在我的安装(2.6.2 Linux)上:

>>> 'X'*10 is 'X'*10
True
>>> 'X'*30 is 'X'*30
False

对于整数也类似:

>>> 2**8 is 2**8
True
>>> 2**9 is 2**9
False

所以不要依赖“字符串”就是“字符串”:即使只看 C 实现也是不安全的。

So there is one and only one place in memory for every Python string?

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.

eg. on my install (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.

望笑 2024-08-11 07:39:32

文字字符串可能根据其哈希值或类似的东西进行分组。两个相同的文字字符串将存储在同一内存中,并且任何引用都引用该字符串。

 Memory        Code
-------
|          myLine = "hello"
|        /
|hello  <
|        \
|          myLine = "hello"
-------

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"
-------
梦里寻她 2024-08-11 07:39:32

如果两个参数是同一个对象,则 is 运算符返回 true。你的结果是这个和引用位的结果。

对于字符串文字,它们是被保留的,这意味着它们与已知字符串进行比较。如果已知相同的字符串,则文字将采用该值,而不是替代值。因此,它们成为同一个对象,并且表达式为真。

The is operator returns true if both arguments are the same object. Your result is a consequence of this, and the quoted bit.

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.

Bonjour°[大白 2024-08-11 07:39:32

Python解释器/编译器解析字符串文字,即引用的字符列表。当它这样做时,它可以检测到“我以前见过这个字符串”,并使用与上次相同的表示形式。它可以这样做,因为它知道以这种方式定义的字符串不能更改。

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.

两仪 2024-08-11 07:39:32

为什么奇怪呢。如果字符串是不可变的,那么只存储一次就很有意义。 .NET 具有相同的行为。

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.

悟红尘 2024-08-11 07:39:32

我认为如果任何两个变量(不仅仅是字符串)包含相同的值,则该值将仅存储一次而不是两次,并且两个变量将指向同一位置。这样可以节省内存。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文