Finally found a use for lambda!

Roy Smith roy at panix.com
Sun Sep 14 22:28:39 EDT 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 :-)




More information about the Python-list mailing list