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

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


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


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'*10True>>> 'X'*30 is 'X'*30False

similarly for ints:

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

So don't rely on 'string' is 'string': even just looking at the C implementation it isn't safe.


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"-------