In
def plus(y): return lambda x: x+y
, how are both arguments passed to this function?
It doesn’t — or at least, plus()
doesn’t pass both arguments into the lambda function. plus(3)
returns lambda x: x + 3
— a function that takes in one argument and increments its argument by 3. This process is known as currying.
To address your example, three(plus(one()))
:
one()
returns1
.plus(one())
becomesplus(1)
and returnslambda x: x + 1
.three(plus(one())
becomesthree(lambda x: x + 1)
.three()
calls the lambda function passed in with an argument of 3 and returns the resulting value. This gives a final result of 4.
CLICK HERE to find out more related problems solutions.