anonymous function with multiple statements

Y

Yingjie Lan

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
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top