[ACCEPTED]-Python: Why does ("hello" is "hello") evaluate as True?-object-comparison
Python (like Java, C, C++, .NET) uses string 4 pooling / interning. The interpreter realises 3 that "hello" is the same as "hello", so 2 it optimizes and uses the same location 1 in memory.
"hell" + "o" is "hello" ==>
So there is one and only one place in memory 9 for every Python string?
No, only ones the 8 interpreter has decided to optimise, which 7 is a decision based on a policy that isn't 6 part of the language specification and which 5 may change in different CPython versions.
eg. on 4 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 3 don't rely on 'string' is 'string': even 2 just looking at the C implementation it 1 isn't safe.
Literal strings are probably grouped based 4 on their hash or something similar. Two 3 of the same literal strings will be stored 2 in the same memory, and any references both 1 refer to that.
Memory Code ------- | myLine = "hello" | / |hello < | \ | myLine = "hello" -------
is operator returns true if both arguments 8 are the same object. Your result is a consequence 7 of this, and the quoted bit.
In the case 6 of string literals, these are interned, meaning 5 they are compared to known strings. If 4 an identical string is already known, the 3 literal takes that value, instead of an 2 alternative one. Thus, they become the 1 same object, and the expression is true.
The Python interpreter/compiler parses the 6 string literals, i.e. the quoted list of 5 characters. When it does this, it can detect 4 "I've seen this string before", and use 3 the same representation as last time. It 2 can do this since it knows that strings 1 defined in this way cannot be changed.
Why is it strange. If the string is immutable 2 it makes a lot of sense to only store it 1 once. .NET has the same behavior.
I think if any two variables (not just strings) contain 4 the same value, the value will be stored 3 only once not twice and both the variables 2 will point to the same location. This saves 1 memory.
More Related questions