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

Re: [pygame] Adding __eq__ method to pygame Event class at runtime



hi,

Sorry, the Event class doesn't let you add attributes(like Nicholas says).

If you have a patch at the C level we can add an __eq__  since it seems like a good idea (I think).  Or maybe someone will implement it for you.

Or maybe we could allow events to have a dictionary associated with them... so you can add methods and attributes like this...  So it acts more like a normal object.  Will have to think about the implications of this...


cheers,




On Tue, Apr 14, 2009 at 9:17 AM, Jordan Applewhite <jordan.applewhite@xxxxxxxxx> wrote:
Hi! 

I thought it would be nice to add an __eq__ method to the pygame.event.Event class at runtime.  This would let me use == (rich comparison operator) on events in the manner of

if pygame.event.wait() == trigger_event:
   pass

I tried to do this with setattr like so (please pardon the one-liner):
setattr( pygame.event.Event.__class__,   '__eq__',  lambda self, other: self.type == other.type )

I've tried several variations on this idea, but the attribute seems to be off-limits.  I keep getting errors like  "TypeError: can't set attributes of built-in/extension type 'builtin_function_or_method'".  I notice that calling pygame.event.Event.__class__ lists it as a "builtin function or method.", but I'm not sure why this means I can't create the method.

Could someone please point me in the right direction?  Thanks, and I'm sorry if this is more about python than pygame.  I'm hoping it passes the relevance threshold:)