Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Finally found a use for lambda!

Reply
Thread Tools

Finally found a use for lambda!

 
 
Roy Smith
Guest
Posts: n/a
 
      09-15-2003
My apologies for a somewhat silly posting, but after 6 years of hacking
Python, I finally found a use for lambda! I wanted to write a unit test
to prove that a given dictionary does not have a given key. Since
assertRaises requires its second argument to be something callable,
instead of writing:

self.assertRaises (KeyError, foo['bar'])

I had to write:

self.assertRaises (KeyError, lambda: foo['bar'])

Of course, now that I think about it, I could have also written:

self.assertEqual (foo.has_key ('bar')), 0)

so I guess I didn't really need the lambda after all
 
Reply With Quote
 
 
 
 
Will Stuyvesant
Guest
Posts: n/a
 
      09-15-2003
> [Roy Smith]
> My apologies for a somewhat silly posting, but after 6 years of hacking
> Python, I finally found a use for lambda!


Not silly at all! Besides in copying example GUI code I have never
found one either.

> ...
> self.assertRaises (KeyError, lambda: foo['bar'])
>
> Of course, now that I think about it, I could have also written:
>
> self.assertEqual (foo.has_key ('bar')), 0)
>
> so I guess I didn't really need the lambda after all


But the lambda version looks so much clearer, and it puts more
emphasis on the KeyError.

And I see you also practice the wonderful elegance of using 0 instead
of False. Booleans?!? Off with their heads!
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      09-15-2003
Roy Smith wrote:

> My apologies for a somewhat silly posting, but after 6 years of hacking
> Python, I finally found a use for lambda! I wanted to write a unit test
> to prove that a given dictionary does not have a given key. Since
> assertRaises requires its second argument to be something callable,
> instead of writing:
>
> self.assertRaises (KeyError, foo['bar'])
>
> I had to write:
>
> self.assertRaises (KeyError, lambda: foo['bar'])
>
> Of course, now that I think about it, I could have also written:
>
> self.assertEqual (foo.has_key ('bar')), 0)


This is not strictly equivalent, i. e. for a custom dictionary you should
perform both tests. If assertEqual() is sufficient, then how about

self.failIf("bar" in foo)

which seems as explicit as you can get without using plain english.

> so I guess I didn't really need the lambda after all


Same goes for me since I converted from map() to list comprehensions.

Peter
 
Reply With Quote
 
=?ISO-8859-1?Q?Hannu_Kankaanp=E4=E4?=
Guest
Posts: n/a
 
      09-15-2003
Roy Smith <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> My apologies for a somewhat silly posting, but after 6 years of hacking
> Python, I finally found a use for lambda! I wanted to write a unit test
> to prove that a given dictionary does not have a given key. Since
> assertRaises requires its second argument to be something callable,
> instead of writing:
>
> self.assertRaises (KeyError, foo['bar'])
>
> I had to write:
>
> self.assertRaises (KeyError, lambda: foo['bar'])
>
> Of course, now that I think about it, I could have also written:
>
> self.assertEqual (foo.has_key ('bar')), 0)
>
> so I guess I didn't really need the lambda after all


Lambda is pretty useless in Python, unfortunately. List comprehensions
reduce their uses with filter and map, and any more complex closure
has to be written as a named function anyway.. I wish lambda would
allow statements in them also, so I could do

func(lambda x: arr[x] = y)

Little closures like that are sometimes handy. But having to do

def fn(x): arr[x] = y
func(fn)

is not too much of a burden. Lambda would be just nicer.
The current use I've had for lambda has been similar to yours. I
wanted a separate thread to initialize something in a class on
the background, so I did something like

threading.Thread(target=lambda: self.doSomething(a, b, c)).start()

I think I've used it elsewhere too but I don't have my sources
here.
 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      09-15-2003
Roy Smith <(E-Mail Removed)> wrote in news:roy-
http://www.velocityreviews.com/forums/(E-Mail Removed):

> My apologies for a somewhat silly posting, but after 6 years of hacking
> Python, I finally found a use for lambda! I wanted to write a unit test
> to prove that a given dictionary does not have a given key. Since
> assertRaises requires its second argument to be something callable,
> instead of writing:
>
> self.assertRaises (KeyError, foo['bar'])
>
> I had to write:
>
> self.assertRaises (KeyError, lambda: foo['bar'])
>


Pretty much the same idea hit me a few weeks ago. Even when the argument is
callable with arguments, using lambda here makes the test read more clearly
than you get without.

I think you should submit a change request to the documentation for
assertRaises/failUnlessRaises that would add another paragraph pointing out
that using a lambda for the callable expression allows any expression to be
tested to be written inline.

--
Duncan Booth (E-Mail Removed)
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
 
Reply With Quote
 
Follower
Guest
Posts: n/a
 
      09-15-2003
> Of course, now that I think about it, I could have also written:
> self.assertEqual (foo.has_key ('bar')), 0)


ObMinimalism attempt:

self.failIf(foo.has_key('bar'))
 
Reply With Quote
 
Andrew Bennetts
Guest
Posts: n/a
 
      09-15-2003
On Mon, Sep 15, 2003 at 01:33:06AM -0700, Hannu Kankaanp?? wrote:
> Roy Smith <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> >
> > so I guess I didn't really need the lambda after all

>
> Lambda is pretty useless in Python, unfortunately. List comprehensions
> reduce their uses with filter and map, and any more complex closure
> has to be written as a named function anyway.. I wish lambda would
> allow statements in them also, so I could do
>
> func(lambda x: arr[x] = y)


Well, you *could* do func(lambda x: arr.__setitem__(x, y)). But you
probably shouldn't

-Andrew.


 
Reply With Quote
 
=?ISO-8859-1?Q?Hannu_Kankaanp=E4=E4?=
Guest
Posts: n/a
 
      09-15-2003
(E-Mail Removed) (Follower) wrote in message news:<(E-Mail Removed). com>...
> > Of course, now that I think about it, I could have also written:
> > self.assertEqual (foo.has_key ('bar')), 0)

>
> ObMinimalism attempt:
>
> self.failIf(foo.has_key('bar'))


That's not really minimalism (ObMinimalism=Obfuscation Minimalism?
That even less!). That is actually The way to do it. You can just
read it out loud and it makes sense immediately.
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      09-15-2003
Hannu Kankaanpää wrote:
>
> (E-Mail Removed) (Follower) wrote in message news:<(E-Mail Removed). com>...
> > > Of course, now that I think about it, I could have also written:
> > > self.assertEqual (foo.has_key ('bar')), 0)

> >
> > ObMinimalism attempt:
> >
> > self.failIf(foo.has_key('bar'))

>
> That's not really minimalism (ObMinimalism=Obfuscation Minimalism?
> That even less!). That is actually The way to do it. You can just
> read it out loud and it makes sense immediately.


Some folks prefer to stick with the positive logic (assert X)
always, rather than risk confusion by mixing and matching in an
arbitrary manner.

Personally, I couldn't keep the negative logic of the "fail X"
style straight and had to give it up.

-Peter
 
Reply With Quote
 
Follower
Guest
Posts: n/a
 
      09-16-2003
>(ObMinimalism=Obfuscation Minimalism?
Ob = Obligatory (Old school Usenet idiom...)

This was in reference to the seemingly eternal programmer desire to
reduce key strokes. And, as mentioned elsewhere, this is even fewer:

self.failIf("bar" in foo)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finally found a good use for the 3:2 format RichA Digital Photography 2 07-18-2010 01:48 AM
Try...Catch...Finally not firing finally? David Lozzi ASP .Net 12 05-11-2007 12:41 AM
I think finally I found banding in a D200? Rita Ä Berkowitz Digital Photography 19 03-18-2006 12:42 PM
Finally found how to display confirm dialog box Tony Baker ASP .Net 0 12-17-2003 11:59 AM
Re: Finally found a really helpful site for PC Repair trout Computer Support 1 06-22-2003 01:54 AM



Advertisments