After looking at this question and its duplicate, the question remained for me.
I get what to do is
and ==
, and why, if I run
a = "ab" b = "ab" a == b
I get True
. The question here will be WHY :
a = "ab" b = "ab" a is b # Returns True
So, I did my research, and I found this one . The answer says that the Python interpreter uses a string pool. Therefore, if he sees that the two lines are the same, he assigns the same id
to the new one for optimization.
So far, everything is in order and response. My real question is why this pool only happens for some rows. 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 = "ab" b = "ab" a is b # Returns False, why?? a = "ab" b = "ab" a is b # Returns False, WHY??
So, for some characters, it seems that the string pool is not working. I used Python 2.7.6 for these examples, so I thought it would be fixed in Python 3. But after you try to use the same examples in Python 3, the same results will appear.
Question: Why is the row optimizer not optimized for these examples? Wouldn't it be better for Python to optimize this as well?
Edit: If I run "ab" is "ab"
, return True
. The question is why using variables returns False
for some characters, but True
for others.