C M wrote:
I know nothing about lambda, have never used it yet. Could you explain a
bit about what's going on this code, particularly the point of it, here?:
googling for the python masters explanation may work better, but I'll try...
btn.Bind(wx.EVT_BUTTON, lambda evt, temp=button_name: self.OnButton(evt, temp) )
Bind() takes two (or more) arguments:
the event type: wx.EVT_BUTTON
and the callback. The callback should be a python callable that takes one argument -- the event object. In the usual case, that's easy, something like:
self.OnButton
where:
def OnButton(self, event):
some_stuff
the self is the class instance object, so the event gets passed through as the only argument.
In this case, we are trying to pass some extra info into the callback: button_name. so we use a lamba:
lambda evt temp=button_name:
creates a function that takes two arguments, but only the first is required, the event, and the other has a default value. The way python keyword bindings work is that "temp" is bound to the OBJECT that is bound to button_name *When the function is created* -- that's key, 'cause this is in a loop, and that name is bound to a different object each time through the loop, and a new function object is created each time, each with a different object bound to "temp".
what the lambda function returns is return value of self.OnButton(evt, temp), with temp set to the button_name object.
so, the callback mechanism gets a callable that takes an event as an argument, and the OnButton method gets called with that event, and one other argument.
note that there is nothing special about lambda here -- it just lets you cram it all onto one line. You could do:
def callback(evt temp=button_name):
return self.Onbutton(evt, temp)
btn.Bind(wx.EVT_BUTTON, callback )
and it would mean exactly the same thing.
HTH,
-Chris
···
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov