11

I'm learning about lambda functions in Python through tutorials online. I understand how it works but I came across an example that puzzles me (on this page):

def myfunc(n):
    return lambda a : a * n

mydoubler = myfunc(2)
print(mydoubler(11))

I don't understand how mydoubler function works here. How does it take 11 as an argument when we didn't define it before?

3
  • 6
    mydoubler is a reference to the lambda returned by myfunc, and takes an a as input. 11 is passed as this a. Commented Feb 16, 2019 at 19:25
  • 4
    mydoubler is lambda a: a * n, and so 11 is a, the only argument of that lambda. Commented Feb 16, 2019 at 19:25
  • 3
    you’re essentially making a curried function here Commented Feb 16, 2019 at 19:54

10 Answers 10

7

mydoubler is what myfunc(2) returns. It returns a lambda that accepts a single argument, a.

When you call on a function like this: myfunction(this_argument), it is going to resolve to what is returned in that spot. So this is effectively the same as writing mydoubler = lambda a : a * 2

Sign up to request clarification or add additional context in comments.

Comments

4

A lambda function is a small anonymous function. In your example

myfunc(2) # lambda a: a * 2

You translate it as apply a function on each input element. It is quite obvious when an input is just a scalar, for example

mydoubler(11) #outputs 11 * 2 = 22

But what do you expect when the input is an array or a string?

mydoubler([1,1,1]) #outputs [1,1,1] * 2 = [1,1,1,1,1,1]
mydoubler("str") #outputs "str" * 2 = "strstr"

Comments

2

Your example has two functions: the outer function myfunc and the inner function lambda. Normally you can call a lambda function directly:

n = 2
print((lambda a: a * n)(11))
# 22

Or you can assign some variable to this function and call it through this variable:

inner = lambda a: a * n
print(inner(11))
# 22

You can also define some outer function, which will return the inner lambda function:

def myfunc():
    n = 2
    return lambda a: a * n

mydoubler = myfunc()
print(mydoubler(11))
# 22

What is equivalent to:

mydoubler = lambda a: a * 2
print(mydoubler(11))
# 22

In the example above the variable n was declared inside myfunc and in your case n is the parameter of myfunc, which is passed to the lambda function. The function myfunc returns thelambda function with n equal to the argument, which you pass to myfunc by the function call. So the function call myfunc(2) returns the fuction lambda a: a * 2.

Comments

1

as Python documentation says, lambda is only anonymous function

Lambda expressions (sometimes called lambda forms) are used to create anonymous functions. The expression lambda parameters: expression yields a function object.

you can see it in here

what's going on in your snippet of code is that your myfunc function use n as a constant to new anonymous function that receive one parameter called a and return the multiplication of a with the n. In your calling n value is 2, result by your call myfunc(2). when you call mydoubler(11) you call your new anonymous function when a has value 11

Comments

1
  • As per the lambda documentation in https://pythonreference.readthedocs.io/en/latest/docs/operators/lambda.html

    lambda returns an anonymous function.

  • In the above-mentioned example, lambda function is lambda a : a * n and the lambda itself returns some anonymous function which must be something like

    def mydoubler(a, n):
        return a*n
    
  • But here, as we have already passed n=2 while calling myfun(), hence doing mydoubler(a) just returns a*2 (as here n=2) and hence the result.

Comments

0

Here mydoubler=myfunc(2) #myfunc(2) returns lambda function to mydoubler

 mydoubler=lambda a:a*2
 # it is similar to x=lambda a:a*2

 print(mydoubler(11)
 # it is similar to print(x(11))

Comments

0

When you called the function after assigning mydoubler to it, it returned a function, the lambda FUNCTION, and, hence, mydoubler itself became a function and could be passed in parameters.

1 Comment

Hi @rajs welcome to stackoverflow. This is more of a comment than an answer. You should also take a look at the how to answer page.
0

Had same question and landed here by google search. It seems hard at the beginning but crystal clear once understood.

myfunc defines a lambda function with parameter n which is unknown yet.

mydoubler defines a lambda function using myfunc with by giving value to that parameter n=2.

Keep in mind mydoubler is still a lambda function, so it takes variable(s) to work, here within the print function a=11.

Comments

0

How do we write lambda func in simple 1 line i.e, consider:

x = lambda a: a*3
print(x(2))

the output we expect is 2*3 =6.

Here 2nd line print(x(2)) is nothing but value of a to-be-considered if you write print(x) it will show error as <function myfunc.<locals>.<lambda> at 0x000001F2D170AD40> or something.

same thing in

def myfunc(n):
    return lambda a : a * n

mydoubler is nothing but (x) = myfunc(2) is nothing but (lambda a:a*2) func, just name used are different. print(mydoubler(11)) is similar to print(x(valueof a))

Comments

-2

To understand this concept the above code can be simplified as given below:

def myfunc(n):
    return lambda a : a*n
mydoubler = myfunc(2)
mydoubler(11)

This is only because lambda is itself a function . myfunc(2) is called and stored in mydoubler . Also myfunc(2) shares parameter i.e 2 with lambda. Lambda and mydoubler gets connected through myfunc(2). When mydoubler is called and passed with equal parameter to lambda,Lambda function will be automatically called.

Lambda function are the function which are defined without a name . There for it is also called anonymous function.

2 Comments

In this code, myfunc doesn't return anything and mydoubler is not defined.
on the same lines as @wjandrea, can you elaborate on the myfunc(2) or mydoubler(11) will illustrate these concepts?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.