[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] Constant identity



Magnus Lie Hetland wrote:
> I think I've asked about this before, but is it safe to rely on
> constant identity in Pygame? I.e. is it OK to say
> 
>   if event.type is QUIT: ...

generally it is not safe to do this. equal python integers do not 
necessarily point to to the same python object (which "is" tests for).

now in 'real life', python has a 'cache' for all numbers from -1 to 
99. so all "low integers" will magically point to the same python 
object. this is not 'guaranteed' by any means, but it has always 
been (and will perhaps always be?)

sdl only allows for up to NUMEVENT events, which is 32. so this 
logically means any pygame event types will always point to the same 
python object. but this won't work for things like key types, like 
K_DELETE.

therefore:
     event.type is QUIT       #not recommended, but works
     event.key is K_BACKSPACE #don't do it

in the python interpreter..
 >>> 10 is 10*1
1
 >>> 100 is 100*1
0

in the end, stick with the equals operator " == " and everyone wins.

____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org