[Tutor] fancy list things
alan.gauld at blueyonder.co.uk
Fri Feb 6 16:30:36 EST 2004
> > In Python lambda can be used to build simple (very simple sadly)
> > anonymous expressions (no statements) which can get used at places
> > you think it's not worth writing a function with a name.
> Is it only useful in map() and filter() and reduce()?
> And other places where you want to hand a little function to a
> comp() for example.
> Is there another class of examples?
GUI programming where you want a widget to do something simple
- like call another method with a predetermined set of values, eg:
# define lots of other widgets here
self.doIt = Button(parent, text="Doit",
command=lambda : self.aFancyOne(23,42,"DoIt
self.doAnother = Button(parent, ext = "Another",
command = lambda :
So we see a method taking two parameters being called from the
command functions of two separate widgets with two different
sets of values.
We could have defined two small functions and passed them instead
but it gets messy:
def DoItFunc(self): return self.aFancyOne(23,42,"DoIt called")
def AnotherFunc(self): return self.aFancyOne(1000,-3,"Help!")
then define the widgets like this:
self.doIt = Button(parent, text="Doit", command=DoItFunc)
self.doAnother = Button(parent, ext = "Another", command =
lambda just saves a little typing and keeps the namespace from
getting cluttered. It also meands the hard coding is kept at the
point of use rather than requiring maintenance to be spread over
two parts of the program...
More information about the Tutor