Python is not ==

Published 2011-10-13 on Farid Zakaria's Blog

Tricky semantics

Getting back into Python for work, I've been writing a Django application. When you write Python, I tend to always want to try and write it in the most 'Pythonic' way. To me, the word represents at it's basic form writing Python code in a way that reads itself easily; nearly as English. I'm sure I'm completely wrong and that it only represents awesome crazy list comprehensions...

A common neologism in the Python community is pythonic, which can have a wide range of meanings related to program style. To say that code is pythonic is to say that it uses Python idioms well, that it is natural or shows fluency in the language.

With the prerequisite of a perhaps faulty state of mind, I tend to use the keywords not and is abundantly in my boolean expressions. The is keyword however stumped me when used on Strings. The following two if statements are not identical. In fact, using the is keyword will likely fail the boolean expression.

if a is "test":
    print "Is Found"
if a == "test":
    print "Equality Found"

Why was this tricky however? Once I noticed the condition failing, I did my favourite thing to do when debugging Python, I launched the console!

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = "test"
>>> if a is "test":
... print "Is matched"
Is matched

Didn't take too long Googling to get to the bottom of this however, and as always Stackoverflow provided a nice answer.

The Python is keyword tests object identity. You should NOT use it to test for string equality. It may seem to work frequently because Python implementations, like those of many very high level languages, performs "interning" of strings. That is to say that string literals and values are internally kept in a hashed list and those which are identical are rendered as references to the same object. (This is possible because Python strings are immutable).

I put the header as tricky, however I guess at the end of the day it's my fault for not knowing the language better.