Lambda inside lambda
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)
You aren't actually calling the inner lambda
:
p = lambda x: (lambda x: x%2)(x)/2
Note in Python 2 this example will always return 0
since the remainder from dividing by 2
will be either 0
or 1
and integer-dividing that result by 2
will result in a truncated 0
.