MMM
MMM

Reputation: 305

Using lambda inside a function

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?

Upvotes: 11

Views: 11188

Answers (10)

user17895809
user17895809

Reputation: 1

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))

Upvotes: 0

Ruming Jiang
Ruming Jiang

Reputation: 1

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.

Upvotes: 0

rajs
rajs

Reputation: 1

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.

Upvotes: 0

Sangam Prasad Yadav
Sangam Prasad Yadav

Reputation: 1

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.

Upvotes: -2

Anurag Dingar
Anurag Dingar

Reputation: 1

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))

Upvotes: 0

Johnny
Johnny

Reputation: 9529

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"

Upvotes: 4

Mykola Zotko
Mykola Zotko

Reputation: 17911

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.

Upvotes: 2

Shivam
Shivam

Reputation: 51

  • 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.

Upvotes: 1

hanrok
hanrok

Reputation: 29

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

Upvotes: 1

Joshua Schlichting
Joshua Schlichting

Reputation: 3460

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

Upvotes: 7

Related Questions