Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda inside lambda

Tags:

python

lambda

Just for curiosity. Discovered Lambdas a few days ago. I was jus wondering if something like that can be done:

(Tried on the interpret but none of my tries seemed to work)

p = lambda x: (lambda x: x%2)/2

There's no explicit purpose. I just did'nt find a satisfactory answer. I may have misunderstood Lambdas.

like image 321
Juan Gallostra Avatar asked May 31 '13 12:05

Juan Gallostra


Video Answer


1 Answers

You can use an inner lambda to return another function, based on the outer parameters:

mul = lambda x: (lambda y: y * x)
times4 = mul(4)
print times4(2)
like image 180
Thomas Fenzl Avatar answered Oct 07 '22 07:10

Thomas Fenzl