Finally found a use for lambda!

Discussion in 'Python' started by Roy Smith, Sep 15, 2003.

  1. Roy Smith

    Roy Smith Guest

    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 :)
    Roy Smith, Sep 15, 2003
    #1
    1. Advertising

  2. > [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!
    Will Stuyvesant, Sep 15, 2003
    #2
    1. Advertising

  3. Roy Smith

    Peter Otten Guest

    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
    Peter Otten, Sep 15, 2003
    #3
  4. Roy Smith <> wrote in message news:<>...
    > 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.
    =?ISO-8859-1?Q?Hannu_Kankaanp=E4=E4?=, Sep 15, 2003
    #4
  5. Roy Smith

    Duncan Booth Guest

    Roy Smith <> wrote in news:roy-
    :

    > 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
    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?
    Duncan Booth, Sep 15, 2003
    #5
  6. Roy Smith

    Follower Guest

    > 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'))
    Follower, Sep 15, 2003
    #6
  7. On Mon, Sep 15, 2003 at 01:33:06AM -0700, Hannu Kankaanp?? wrote:
    > Roy Smith <> wrote in message news:<>...
    > >
    > > 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.
    Andrew Bennetts, Sep 15, 2003
    #7
  8. (Follower) wrote in message news:<>...
    > > 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.
    =?ISO-8859-1?Q?Hannu_Kankaanp=E4=E4?=, Sep 15, 2003
    #8
  9. Roy Smith

    Peter Hansen Guest

    Hannu Kankaanpää wrote:
    >
    > (Follower) wrote in message news:<>...
    > > > 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
    Peter Hansen, Sep 15, 2003
    #9
  10. Roy Smith

    Follower Guest

    >(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)
    Follower, Sep 16, 2003
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Baker
    Replies:
    0
    Views:
    6,688
    Tony Baker
    Dec 17, 2003
  2. Roman Suzi
    Replies:
    13
    Views:
    594
    Bengt Richter
    Jan 7, 2005
  3. David Lozzi

    Try...Catch...Finally not firing finally?

    David Lozzi, Apr 23, 2007, in forum: ASP .Net
    Replies:
    12
    Views:
    783
    Alvin Bruney [MVP]
    May 11, 2007
  4. Steve Dogers

    lambda vs non-lambda proc

    Steve Dogers, Mar 30, 2009, in forum: Ruby
    Replies:
    1
    Views:
    159
    Sean O'Halpin
    Mar 30, 2009
  5. Haochen Xie
    Replies:
    4
    Views:
    234
    Haochen Xie
    Mar 17, 2013
Loading...

Share This Page