Finally found a use for lambda!

R

Roy Smith

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 :)
 
W

Will Stuyvesant

[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!
 
P

Peter Otten

Roy said:
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?=

Roy Smith said:
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.
 
D

Duncan Booth

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.
 
F

Follower

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'))
 
A

Andrew Bennetts

Roy Smith said:
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?=

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.
 
P

Peter Hansen

Hannu said:
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
 
F

Follower

(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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top