class - Python: Bind an Unbound Method? -
in python, there way bind unbound method without calling it?
i writing wxpython program, , class decided it'd nice group data of of buttons class-level list of tuples, so:
class mywidget(wx.window): buttons = [("ok", onok), ("cancel", oncancel)] # ... def setup(self): text, handler in mywidget.buttons: # following line problem line. b = wx.button(parent, label=text).bind(wx.evt_button, handler)
the problem is, since of values of handler
unbound methods, program explodes in spectacular blaze , weep.
i looking around online solution seems should relatively straightforward, solvable problem. unfortunately couldn't find anything. right now, i'm using functools.partial
work around this, know if there's clean-feeling, healthy, pythonic way bind unbound method instance , continue passing around without calling it?
all functions descriptors, can bind them calling __get__
method:
bound_handler = handler.__get__(self, mywidget)
here's r. hettinger's excellent guide descriptors.
Comments
Post a Comment