diliup gabadamudalige wrote:
Can someone please explain why
if event.type is KEYUP:
is bad and
if event.type == KEYUP:
is correct?
The 'is' operator tests whether two expressions refer to
the *same* object. It's possible for two different int
objects to have the same value, in which case 'is' and
'==' will give different results, e.g.
>>> 666 == 2 * 333
True
>>> 666 is 2 * 333
False
You can be misled if you try this experiment with
sufficiently small integers, however:
>>> 6 is 2 * 3
True
This happens because CPython keeps a cache of small
integer objects and re-uses them. But that's strictly
an implementation detail, and not something you
should rely on. The only reliable way to tell whether
two ints are equal is to use ==.
--
Greg