Python: Why does ("hello" is "hello") evaluate as True? [duplicate]
Asked Answered
C

7

63

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?

Cavetto answered 8/9, 2009 at 7:13 Comment(2)
Also have a look at the id function for checking memory locations: print id("hello")Cerography
bzlm, the pyref.infogami.com/intern link has gone dead, but archive.org has a copy here: <br/> web.archive.org/web/20090429040354/http://pyref.infogami.com/… <br/> However, though it's often true, it's NOT ALWAYS true, as @bobince demonstrated very well below.Skite
P
95

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

Polygynous answered 8/9, 2009 at 7:15 Comment(4)
Even C/C++ usually do this; "foo" == "foo" is often true in C. In both C and Python, this is an implementation detail; I don't think anything in Python requires that the interpreter do this, and in C/C++ this is an optimization that not all compilers do and it which can be disabled. (By contrast, this property is always true in Lua; all strings are interned.)Taishataisho
@Glenn, you're correct and I'm glad someone mentioned. Certainly no one should RELY on this being true.Carillonneur
It is an interpreter or compiler for languages like c/C++ specific job to do this optimization by making compile time determined strings the same.Caller
In this specific case, the objects are the same because the two literals in the same expression match and result in a single constant stored in the code. If you used a = 'hell' + 'o!' and b = 'hello!' on separate lines in the interactive shell, a is b is going to be false. a = 'hell' + 'o' and b = 'hello'` does trigger interning, so it'll be true. But put the two examples into a function, and you'll have identical objects again. There are multiple paths to object reuse and they invariably are the result of optimisations. Don't rely on implementation details like these.Thanos
T
66

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.

Tana answered 8/9, 2009 at 9:19 Comment(3)
Thus, you should always use == for string equality comparisons.Yapon
Interpreter caches small integers(upto 256) in Python. So, a = 50; b = 50; a is b is True, a = 500; b = 500; a is b is False.Antipyretic
@DarshanChaudhary: the latter expression is actually True, because you put all your assignments one one line. 500 is a literal that's stored as a constant in the code object, and both a and b are assigned that one constant... Again, implementation detail, don't count on it.Thanos
J
13

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"
-------
Johnsten answered 8/9, 2009 at 7:16 Comment(2)
This is exactly what the accepted answer says...Genital
As bobince pointed out, this isn't necessarily always true.Fable
Y
6

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.

Yapon answered 8/9, 2009 at 7:16 Comment(2)
They "become the same object"? If you modify one, the other isn't modified.Inscription
@endolith: The object in question is the interned string, not the variable assigned to that string. There is no way, in python, to modify a string.Yapon
H
2

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.

Harlow answered 8/9, 2009 at 7:16 Comment(0)
P
1

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.

Pampa answered 8/9, 2009 at 7:14 Comment(3)
How is string interning related to immutability? Many things in both Python and ".NET" are immutable without being interned.Okra
Because if it were possible for a string literal to change in memory, it couldn't be shared (or "interned").Ozzy
True, but given the fact the object is immutable allows safe sharing of the reference to the instance.Pampa
C
0

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.

Craven answered 8/9, 2009 at 9:20 Comment(1)
Not true! It regards only strings and small integers. When you make a copy of a list or dictionary, for example, although they have the same value (== equality) they are not the same object ("is" equality). That is why you can change the copy of the list as the original stays unchanged (or vice versa). The great explanation is provided in Dynamic Typing chapter of Learning Python by O'reillyCrackle

© 2022 - 2024 — McMap. All rights reserved.