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?
Strings in python are surrounded by either single quotation marks, or double quotation marks. 'hello' is the same as "hello".
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.
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'*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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With