[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Re: Inconsistent Constant Comparison



> >>> K_6
> 54
> >>> 54 is K_6
> True
> >>> K_KP4
> 260
> >>> 260 is K_KP4
> False
>
> Is this due to some inconsistency in the way
> the constants were declared?


This is actually do to some sneaky internal optimization by the Python interpreter. low integer values are cached objects, so they always refer to the same object. higher integer objects are instanced like regular python objects, so they do not refer to the same objects.

>>> 2+2 is 4
True
>>> 100+100 is 200
False

the moral of the story is, use the equals operator to compare integers (and the pygame constants, which are integers)