0

I am learning Python from a site and there they have mentioned these lines of code for explaining lambda function

def first_number(n):
    return lambda first : first * n
    
second_num = first_number(2)

print(second_num(20))

Output is 40

How come that the code prints 40 and what values will be taken for variables 'first' and 'n', can anyone please explain this.

  • `first_number(2)` returns a functions. You then call this function with the value `20`. The returned function calculates `2 * 20`. What part exactly confuses you? Did you try to read about lambdas? – Tomerikoo Dec 17 '20 at 08:20
  • Does this answer your question? [Why are Python lambdas useful?](https://stackoverflow.com/questions/890128/why-are-python-lambdas-useful) – Tomerikoo Dec 17 '20 at 08:22

4 Answers4

1

Just to briefly explain how this code works in your case: When assigning first_number(2) to second_num on this line:

second_num = first_number(2)

you called the function first_number and replaced n with 2. The result is assigned to second_num.

Now, the function first_number doesn't just return a value like a constant: it returns a function, because lambda is a function with no name (read further about how lambda works) and you just replaced n with 2 and assigned that function to second_num.

A function returning a function... kinda weird and cool at the same time don't you think? ;)

So you could say that this line:

second_num = first_number(2)

actually becomes (or you could at least interpret it this way):

def second_num(first):
    return first * 2 #the 2 you passed in first_number

So after running second_num(20), it returns 40, obviously.

So what is lambda:

As told, lambda is an unnamed function, so a function with no name. You could interpret this line:

lambda first: first * n

as a function like:

def thisisafunction(first):
    return first*n

So right after lambda are the parameters, comma-separated. And after the colon : is the body of the function.

Another way of applying lambda functions is to save some code. Like Python's filter function (https://docs.python.org/3/library/functions.html#filter), which asks for a function as a parameter. It saves you quite some code this way:

numbers_list = [2, 6, 8, 10, 11, 4, 12, 7, 13, 17, 0, 3, 21]

filtered_list = list(filter(lambda num: (num > 7), numbers_list))

print(filtered_list)

vs

numbers_list = [2, 6, 8, 10, 11, 4, 12, 7, 13, 17, 0, 3, 21]

def filterfunction(num):
    return num > 7

filtered_list = list(filter(filterfunction, numbers_list))
    
print(filtered_list)

Hopefully this makes things clear!

Erik van de Ven
  • 4,747
  • 6
  • 38
  • 80
  • Thanks @Erik for our reply, can you explain as how does the variables take the values? – venkatraman Balasubramanian Dec 17 '20 at 08:01
  • Could you be more specific what you mean with how it takes the variables? You could see `n` as a placeholder, you need to replace it with an actual value. The `first` variable is an actual argument. So the parameter you pass into the function is `first`! – Erik van de Ven Dec 17 '20 at 08:08
  • @venkatramanBalasubramanian guess I understood your question. Updated my answer, hopefully this makes stuff clear. – Erik van de Ven Dec 17 '20 at 08:22
  • 1
    I understood now that your first point "first_number(2)" returns lambda function to "second_num". I hope it sets 'n' as 2 and it will not change till the program execution gets over. Even I was little confused about the second point, but that is also cleared, thanks a lot @Erik – venkatraman Balasubramanian Dec 17 '20 at 10:18
  • You're absolutely welcome. Glad it cleared things up! – Erik van de Ven Dec 17 '20 at 10:30
0

Looking at just the lambda

lambda first : first * n

Is equivalent to a function:

def fn(first):
    return first * n

first is the argument, then it returns the result of the expression after the colon. The n comes from the closure which is not lambda specific.

dpwr
  • 2,732
  • 1
  • 23
  • 38
0

n is 2 and first is 20. Notice that

def first_number(n):
    return lambda first : first * n

is a function that itself returns a function (functions are first-class objects in Python)! Thus

second_num = first_number(2)

is the rough equivalent of

def second_num(first):
    return first * 2

where the n from inside the first function's execution time is fixed to 2 and still accessible as a closure variable.

user2390182
  • 72,016
  • 6
  • 67
  • 89
0
def first_number(n):
    return lambda first : first * n

is similar to

def first_number(n):
    def lambda_fuc(first):
        return first * n
    return lambda_fuc

so,

second_num = first_number(2)  ==> means n=2
print(second_num(20))         ==> means first=20

is similar to

def first_number(n=2):
    def lambda_fuc(first=20):
        return first * n
    return lambda_fuc

we get 40

xiaoxu
  • 29
  • 5