anonymous function with multiple statements

Discussion in 'Python' started by Yingjie Lan, Jul 10, 2011.

  1. Yingjie Lan

    Yingjie Lan Guest

    Hi all,

    I wonder if Python provides a way to define anonymous functions containing
    multiple statements? With lambda form, we can only define a function of a single
    expression. In Javascript, it is possible to define
    a full-fledged anonymous functions, which suggests it is useful to have it. In
    Python, it might be like this:


    #==============

    def test( fn, K ):
    return sum(fn(i) for i in range(K))/K

    test( def (i): #indent from start of this line, not from 'def'
    from math import sin
    #the last statement ends with a ',' or ')'.
    return sin(i)+i*i;, 100) #';' to mark end of statement

    #another way:
    test( def (i): #indent from start of this line, not from 'def'
    from math import sin
    return sin(i)+i*i
    , 100) #freely place ',' anywhere

    #===============

    Any thoughts?

    Yingjie
    Yingjie Lan, Jul 10, 2011
    #1
    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. Neil Zanella
    Replies:
    8
    Views:
    1,165
    mfmehdi
    Oct 20, 2006
  2. Harry George
    Replies:
    6
    Views:
    360
    Bart Nessux
    Feb 23, 2004
  3. Reporter
    Replies:
    3
    Views:
    461
    Mike Schilling
    May 12, 2007
  4. Vince
    Replies:
    12
    Views:
    736
    Martin Gregorie
    Jan 21, 2008
  5. Replies:
    1
    Views:
    211
Loading...

Share This Page