kmario23
kmario23

Reputation: 61375

Understanding nested lambda function behaviour in python

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 variable x. Can somebody explain this nested lambda 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

Upvotes: 5

Views: 21108

Answers (5)

C Panda
C Panda

Reputation: 3405

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:

  1. You are giving an anonymous function a name
  2. 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. :)

Upvotes: 2

hilberts_drinking_problem
hilberts_drinking_problem

Reputation: 11602

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)

Upvotes: 2

apr
apr

Reputation: 390

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

Upvotes: 1

João Paulo
João Paulo

Reputation: 6690

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

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798814

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.

Upvotes: 2

Related Questions