Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 963
Deniz Dogan Avatar asked Sep 08 '09 07:09

Deniz Dogan


People also ask

Is hello is same as hello in Python?

Strings in python are surrounded by either single quotation marks, or double quotation marks. 'hello' is the same as "hello".

How to check equals in Python?

Python strings equality can be checked using == operator or __eq__() function. Python strings are case sensitive, so these equality check methods are also case sensitive.


2 Answers

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

like image 76
carl Avatar answered Oct 01 '22 18:10

carl


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.

like image 44
bobince Avatar answered Oct 01 '22 20:10

bobince