[Tutor] fancy list things
marilyn at deliberate.com
Fri Feb 6 16:49:43 EST 2004
YES! That's a great example! I feel much better about it now.
Thank you again.
On Fri, 6 Feb 2004, Alan Gauld wrote:
> > > 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
> > function?
> > 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:
> class MyGUI:
> def aFancyOne(self,x,y,z)
> def buildGUI(self)
> # 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...
> Alan G.
More information about the Tutor