Understanding nested lambda function behaviour in python
Solution 1
The inner lambda creates a function when the outer lambda is called. The outer lambda returns this function. This function is then called with the argument 2
.
Solution 2
It's kind of a rule (or convention), if you follow the style guide, not to use lambda
in the context you have used. The reason for this is exactly what made you turn to the internet in confusion. The flags are:
- You are giving an anonymous function a name
- There are multiple
lambda
. For a language that does not have nested anonymous functions, this is a code smell. There must be a better way
How about writing this way and reaping the benefits:
def function_product(F, m): # returns inner, a function
def inner(x): # takes x, and closes over F and m from
return F(x)*m # outer scope, hence a closure
return inner
See, everything is clear because it's more readable now. Avoid lambda
, maybe except for callbacks,like in sorted()
. Normal functions are objects, just like 10
and hello
are.
Always do import this
. :)
Solution 3
A good place to start would be to apply type
to your definitions and see if it clarifies things. Also, I can't help but remark that something like Haskell would be a nicer place to start if you are interested in functional programming, even if you do not plan on using the language. That being said, here is what you get:
In [13]: type(square_func)
Out[13]: function
In [14]: type(function_product)
Out[14]: function
In [15]: type(square_func(2))
Out[15]: int
In [16]: type(function_product(square_func, 3))
Out[16]: function
In [17]: type(function_product(square_func, 3)(2))
Out[17]: int
So the puzzling part is the return type of function_product(square_func, 3)
, which is a function itself, one that is presumably intended to take a single number and return a single number. You could write it as:
f = function_product(square_func, 3)
f(2)
Solution 4
function_product
asks for a function and a variable, you passed square_func
and 3. Then, the inner function of function_product
takes the result of the function you passed (square_func
) and multiply it by the variable you passed, so:
square_func(2)*3 = 12
Solution 5
function_product(square_func, 3)
returns other lamba function which can be defined implicitly like this:
lambda x: square_func(x)*3
next by calling the other lamba function you pass 2 to the variable x
kmario23
Journeyman in Python, NumPy/CuPy/SciPy, C/C++, Fortran, (Multi) Linear Algebra, Computer Vision, Multimodal Learning, Deep Learning, PyTorch, Tensors, TensorFlow, Matplotlib, LaTeX, and Vim. Above all, I see Gaussians everywhere!!! Übung macht den Meister Nice Reads: A delightful NumPy guide for Scientific Computing Python PEP A fast-paced primer on Matplotlib Git Ready Hangouts: Python-Chatroom, C++Chatroom Support: If you're a kind Samaritan and want to support me, please do so, for which I would eternally be thankful! paypal.me/kmario23
Updated on June 18, 2022Comments
-
kmario23 almost 2 years
I'm trying to learn pure functional programming. But this code is puzzling me particularly the second line. I do not understand how the value
2
is passed to the variablex
. Can somebody explain this nestedlambda
behaviour?>>> square_func = lambda x: x**2 >>> function_product = lambda F, m: lambda x: F(x)*m >>> square_func(2) 4 >>> function_product(square_func, 3)(2) 12