I have recently discovered a potential bug in a production system where two strings were compared using the identity operator, eg:
if val[2] is not 's':
I imagine this will however often work anyway, because as far as I know CPython stores the short immutable strings in the same location. I've replaced it with !=
, but I need to confirm that the data that previously went through this code is correct, so I'd like to know if this always worked, or if it only sometimes worked.
The Python version has always been 2.6.6 as far as I know and the above code seems to be the only place where the is
operator was used.
Does anyone know if this line will always work as the programmer intended?
edit: Because this is no doubt very specific and unhelpful to future readers, I'll ask a different question:
Where should I look to confirm with absolute certainty the behaviour of the Python implementation? Are the optimisations in CPython's source code easy to digest? Any tips?