[英]Using lambda inside a function
I'm learning about lambda functions in Python through tutorials online.我正在通过在线教程学习 Python 中的 lambda 函数。 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.我不明白mydoubler
函数在这里是如何工作的。 How does it take 11 as an argument when we didn't define it before?当我们之前没有定义它时,它如何将 11 作为参数?
mydoubler
is what myfunc(2)
returns. mydoubler
是myfunc(2)
返回的内容。 It returns a lambda that accepts a single argument, a
.它返回一个接受一个参数,一个lambda a
。
When you call on a function like this: myfunction(this_argument)
, it is going to resolve to what is returned in that spot.当您调用这样的函数时: myfunction(this_argument)
,它将解析为该位置返回的内容。 So this is effectively the same as writing mydoubler = lambda a : a * 2
所以这实际上与编写mydoubler = lambda a : a * 2
A lambda
function is a small anonymous function. lambda
函数是一个小的匿名函数。 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
?但是当输入是array
或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
.您的示例有两个函数:外部函数myfunc
和内部函数lambda
。 Normally you can call a lambda
function directly:通常你可以直接调用一个lambda
函数:
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:您还可以定义一些外部函数,它将返回内部lambda
函数:
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.在上面的例子中,变量n
是在myfunc
中声明的,在你的例子中n
是myfunc
的参数,它被传递给lambda
函数。 The function myfunc
returns the lambda
function with n
equal to the argument, which you pass to myfunc
by the function call.函数myfunc
返回n
等于参数的lambda
函数,该参数是您通过函数调用传递给myfunc
的。 So the function call myfunc(2)
returns the fuction lambda a: a * 2
.所以函数调用myfunc(2)
返回函数lambda a: a * 2
。
as Python documentation says, lambda is only anonymous function正如 Python 文档所说,lambda 只是匿名函数
Lambda expressions (sometimes called lambda forms) are used to create anonymous functions. Lambda 表达式(有时称为 lambda 形式)用于创建匿名函数。 The expression lambda parameters: expression yields a function object.表达式 lambda 参数:表达式产生一个函数对象。
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
.您的代码片段中发生的事情是您的myfunc
函数使用n
作为新匿名函数的常量,该函数接收一个名为a
参数并返回a
与n
的乘法。 In your calling n
value is 2
, result by your call myfunc(2)
.在您调用n
值为2
,您调用myfunc(2)
。 when you call mydoubler(11)
you call your new anonymous function when a
has value 11
当您调用mydoubler(11)
您会在a
值为11
时调用新的匿名函数
As per the lambda documentation in https://pythonreference.readthedocs.io/en/latest/docs/operators/lambda.html根据https://pythonreference.readthedocs.io/en/latest/docs/operators/lambda.html 中的 lambda 文档
lambda returns an anonymous function. lambda 返回一个匿名函数。
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在上面提到的例子中,lambda 函数是 lambda a : a * n 并且 lambda 本身返回一些匿名函数,它必须是这样的
def mydoubler(a, n): return a*n
Here mydoubler=myfunc(2) #myfunc(2) returns lambda function to mydoubler这里 mydoubler=myfunc(2) #myfunc(2) 将 lambda 函数返回给 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))
当您在将 mydoubler 分配给它后调用该函数时,它返回一个函数,即 lambda FUNCTION,因此,mydoubler 本身成为一个函数,可以传入参数。
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. myfunc 定义了一个带参数 n 的 lambda 函数,该函数目前还未知。
mydoubler defines a lambda function using myfunc with by giving value to that parameter n=2. mydoubler 使用 myfunc with 定义了一个 lambda 函数,方法是为该参数 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.请记住,mydoubler 仍然是一个 lambda 函数,因此它需要变量才能工作,这里是在打印函数 a=11 中。
How do we write lambda func in simple 1 line ie, consider:我们如何在简单的 1 行中编写 lambda func,即考虑:
x = lambda a: a*3
print(x(2))
the output we expect is 2*3 =6
.我们期望的输出是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.这里第二行print(x(2))
只不过是要考虑的值,如果你写print(x)
它将显示错误为<function myfunc.<locals>.<lambda> at 0x000001F2D170AD40>
或其他东西。
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. mydoubler
是(x) = myfunc(2)
只不过是(lambda a:a*2)
func,只是使用的名称不同。 print(mydoubler(11))
is similar to print(x(valueof a))
print(mydoubler(11))
类似于print(x(valueof a))
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 .这只是因为 lambda 本身就是一个函数。 myfunc(2) is called and stored in mydoubler . myfunc(2) 被调用并存储在 mydoubler 中。 Also myfunc(2) shares parameter ie 2 with lambda.此外,myfunc(2) 与 lambda 共享参数即 2。 Lambda and mydoubler gets connected through myfunc(2). Lambda 和 mydoubler 通过 myfunc(2) 连接。 When mydoubler is called and passed with equal parameter to lambda,Lambda function will be automatically called.当 mydoubler 被调用并以相等的参数传递给 lambda 时,Lambda 函数将被自动调用。
Lambda function are the function which are defined without a name . Lambda 函数是没有名称定义的函数。 There for it is also called anonymous function.因为它也被称为匿名函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.