[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Adding __eq__ method to pygame Event class at runtime
- To: pygame-users@xxxxxxxx
- Subject: [pygame] Adding __eq__ method to pygame Event class at runtime
- From: Jordan Applewhite <jordan.applewhite@xxxxxxxxx>
- Date: Mon, 13 Apr 2009 19:17:54 -0400
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Mon, 13 Apr 2009 19:17:56 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:date:message-id:subject :from:to:content-type; bh=TSqA4udjJ9aewjeEhUsUg1v81nYf+TFjKo1on8gis5k=; b=mllHCoInsi6997ig2Xu0uS//c15f5zJ19eE6JvVcYPvmW1YOLA9p6kRwN/Txa7FxBZ Ris+cdTsqKCgIkO6iyYWBxCCi0mHr7pw6uf4ixtm4cM1uL7OFygCDKQG5s2P1mhXAPaf sxjBfvJFI+xbrAFvKCqRJWl6ndZad5Qd+mfN8=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=dO0Ex2Z5q1k4hKLcU/nzPrTuv+Z+sx8SEqQ3fADif6oTEH3RD5tV+0OTsfESbEVYE5 D/P7e1hcmplf8xutVmVqc9sPYt3lkwvbcp7y9oRwldLNY9fT6BcbNrpuKpogPGKQhRh9 asNeHBQ9+Nl+Fb7CV9wngPDJSBwGLym3QQ1yU=
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
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:)