Skip to content

Instantly share code, notes, and snippets.

@clayote
Last active August 29, 2015 14:25
Show Gist options
  • Save clayote/e612e96322f9b287b140 to your computer and use it in GitHub Desktop.
Save clayote/e612e96322f9b287b140 to your computer and use it in GitHub Desktop.
Trying to make a decorator to make triggers lazily
from kivy.event import EventDispatcher
from kivy.clock import Clock
# this works fine
class Burger(EventDispatcher):
def flip(self, *args):
pass
@property
def _trigger_flip(self):
if not hasattr(self, '_real_trigger_flip'):
self._real_trigger_flip = Clock.create_trigger(self.flip)
return self._real_trigger_flip
# this keeps triggering the method on unexpected objects,
# including many that shouldn't have the method at all, eg. floats
def get_trigger(obj, fun):
n = '_real_trigger_' + fun.__name__
if not hasattr(obj, n):
setattr(obj, n, Clock.create_trigger(fun))
return getattr(obj, n)
trigger = lambda fun: property(fget=lambda self: get_trigger(self, fun))
class Fries(EventDispatcher):
def fry(self, *args):
pass
_trigger_fry = trigger(fry)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment