Curiously:
>>> a = 123
>>> b = 123
>>> a is b
True
>>> a = 123.
>>> b = 123.
>>> a is b
False
Seems a is b
being more or less defined as id(a) == id(b)
. It is easy to make bugs this way:
basename, ext = os.path.splitext(fname)
if ext is '.mp3':
# do something
else:
# do something else
Some fnames unexpectedly ended up in the else block. The fix is simple, we should use ext == '.mp3'
instead, but nonetheless if ext is '.mp3'
on the surface seems like a nice pythonic way to write this and it's more readable than the "correct" way.
Since strings are immutable, what are the technical details of why it's wrong? When is an identity check better, and when is an equality check better?
They are fundamentally different.
==
compares by calling the __eq__
methodis
returns true if and only if the two references are to the same objectSo in comparision with say Java:
is
is the same as ==
for objects==
is the same as equals
for objects