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