views:

762

answers:

7

Why is "hello" is "hello" == 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?

A: 

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.

Brian Rasmussen
How is string interning related to immutability? Many things in both Python and ".NET" are immutable without being interned.
bzlm
Because if it were possible for a string literal to change in memory, it couldn't be shared (or "interned").
harto
True, but given the fact the object is immutable allows safe sharing of the reference to the instance.
Brian Rasmussen
+36  A: 

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

carl
This is also known as "string interning" in the Python documentation.
bzlm
thanks - updated.
carl
Straight-forward and easily understandable answer. Thank you.
Deniz Dogan
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.)
Glenn Maynard
@Glenn, you're correct and I'm glad someone mentioned. Certainly no one should RELY on this being true.
Triptych
+1  A: 

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.

unwind
A: 

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"
-------
Quantumplation
This is exactly what the accepted answer says...
Martin
Upvote to counter the evil downvotes
Martin
+4  A: 

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.

TokenMacGuy
They "become the same object"? If you modify one, the other isn't modified.
endolith
@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.
TokenMacGuy
+16  A: 

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.

bobince
Thus, you should always use `==` for string equality comparisons.
TokenMacGuy
A: 

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.

Owais Lone
Why is this downvoted?
Deniz Dogan