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?
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. In this code, myfunc doesn't return anything and mydoubler is not defined.
What are lambda functions in Python? In Python, an anonymous function is a function that is defined without a name. While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.
Python Lambda Function Syntax This function can have any number of arguments but only one expression, which is evaluated and returned. One is free to use lambda functions wherever function objects are required. You need to keep in your knowledge that lambda functions are syntactically restricted to a single expression.
Syntax. Simply put, a lambda function is just like any normal python function, except that it has no name when defining it, and it is contained in one line of code. A lambda function evaluates an expression for a given argument. You give the function a value (argument) and then provide the operation (expression).
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
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"
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With