Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Finally found a use for lambda! (http://www.velocityreviews.com/forums/t322453-finally-found-a-use-for-lambda.html)

Roy Smith 09-15-2003 02:28 AM

Finally found a use for lambda!
 
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 :-)

Will Stuyvesant 09-15-2003 06:06 AM

Re: Finally found a use for lambda!
 
> [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!

Peter Otten 09-15-2003 07:18 AM

Re: Finally found a use for lambda!
 
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

=?ISO-8859-1?Q?Hannu_Kankaanp=E4=E4?= 09-15-2003 08:33 AM

Re: Finally found a use for lambda!
 
Roy Smith <roy@panix.com> wrote in message news:<roy-FFBAC5.22283914092003@reader2.panix.com>...
> 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.

Duncan Booth 09-15-2003 08:46 AM

Re: Finally found a use for lambda!
 
Roy Smith <roy@panix.com> wrote in news:roy-
FFBAC5.22283914092003@reader2.panix.com:

> 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 duncan@rcp.co.uk
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?

Follower 09-15-2003 09:33 AM

Re: Finally found a use for lambda!
 
> 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'))

Andrew Bennetts 09-15-2003 09:54 AM

Re: Finally found a use for lambda!
 
On Mon, Sep 15, 2003 at 01:33:06AM -0700, Hannu Kankaanp?? wrote:
> Roy Smith <roy@panix.com> wrote in message news:<roy-FFBAC5.22283914092003@reader2.panix.com>...
> >
> > 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.



=?ISO-8859-1?Q?Hannu_Kankaanp=E4=E4?= 09-15-2003 01:57 PM

Re: Finally found a use for lambda!
 
follower@iname.com (Follower) wrote in message news:<d2f38965.0309150133.4aef2253@posting.google. 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.

Peter Hansen 09-15-2003 06:00 PM

Re: Finally found a use for lambda!
 
Hannu Kankaanpää wrote:
>
> follower@iname.com (Follower) wrote in message news:<d2f38965.0309150133.4aef2253@posting.google. 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

Follower 09-16-2003 05:16 AM

Re: Finally found a use for lambda!
 
>(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)


All times are GMT. The time now is 02:55 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.