奇怪的是:
>>> a = 123
>>> b = 123
>>> a is b
True
>>> a = 123.
>>> b = 123.
>>> a is b
False
Seems a is b
are more or less defined as id(a) = id(b)
。 很容易这样作:
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?