After seeing this question and its duplicate a question still remained for me.
I get what is
and ==
do and why if I run
a = "ab"
b = "ab"
a == b
I get True
. The question here would be WHY this happens:
a = "ab"
b = "ab"
a is b # Returns True
So I did my research and I found this. The answer says Python interpreter uses string pooling. So if it sees that two strings are the same, it assigns the same id
to the new one for optimization.
Until here everything is alright and answered. My real question is why this pooling only happens for some strings. Here is an example:
a = "ab"
b = "ab"
a is b # Returns True, as expected knowing Interpreter uses string pooling
a = "a_b"
b = "a_b"
a is b # Returns True, again, as expected knowing Interpreter uses string pooling
a = "a b"
b = "a b"
a is b # Returns False, why??
a = "a-b"
b = "a-b"
a is b # Returns False, WHY??
So it seems for some characters, string pooling isn't working. I used Python 2.7.6 for this examples so I thought this would be fixed in Python 3. But after trying the same examples in Python 3, the same results appear.
Question: Why isn't string pooling optimized for this examples? Wouldn't it be better for Python to optimize this as well?
Edit: If I run "a b" is "a b"
returns True
. The question is why using variables it returns False
for some characters but True
for others.
>>> a = "a-b";b = "a-b" >>> a is b True
– HorrifyFalse
– Gaillardia'a b' is 'a b'
evaluates toTrue
– GusTrue
with your example butFalse
when assigning variables – Gaillardiais
for comparing equality, use==
. This is CPython implementation detail hence you shouldn't use it, it can even break in future CPython releases and might not work in other interpreters at all. Another example of this is that complex number literals are cached in PyPy but not in CPython. – That