简体   繁体   English

这个例子背后的逻辑是什么?

[英]What is the logic behind this example?

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

mytripler = myfunc(3)

print(mytripler(11))

I am trying to learn about the lambda function in python.我正在尝试了解 python 中的 lambda function。 I understand the other examples given inside w3schools about the Lambda function.我了解 w3schools 内部给出的关于 Lambda function 的其他示例。 This example, however, I could not wrap my head around.然而,这个例子,我无法理解。 How is it that mytripler(11) multiplies 3 by 11? mytripler(11) 如何将 3 乘以 11?

Note that there is nothing particularly special about lambda.请注意,lambda 并没有什么特别之处。 It's just a convenient method of creating an anonymous function.这只是创建匿名 function 的便捷方法。

If the code had instead been written:如果改为编写代码:

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

It would be the exact same thing.这将是完全相同的事情。 Except now we've given anonymous function a name inner .除了现在我们给匿名 function 命名为inner But you'd discover that you can still write mytripler = myfunc(3) and it works the same way.但是您会发现您仍然可以编写mytripler = myfunc(3)并且它的工作方式相同。

Python understands closures. Python 理解闭包。 If a variable is used (but not modified) inside an inner function, and there is an identically named variable defined in a containing function, then Python realizes that the inner variable refers to whatever value that outer variable has.如果在内部 function 中使用(但未修改)变量,并且在包含 function 中定义了一个同名变量,则 ZA7F5F35426B927411FC9231B56382173 中定义了外部变量所具有的值。

Let's take a step back: What is a function?让我们退后一步:什么是 function?

One definition could be that's a construct where you give it arguments, it does something and it returns a value (Let's ignore for now the cases where you don't give any arguments or where you don't receive any return value)一个定义可能是你给它 arguments 的构造,它做了一些事情并返回一个值(现在让我们忽略你不提供任何 arguments 或你没有收到任何返回值的情况)

In Python, since everything is an object, this kind of construct is a value that can be assigned a name.在 Python 中,由于一切都是 object,因此这种构造是可以指定名称的值。 For example:例如:

>>> def example_function(argument):
...     result = argument * 42
...     return result
... 
>>> other_name_for_example_function = example_function
>>> example_function(3)
126
>>> other_name_for_example_function(3)
126
>>> example_function == other_name_for_example_function
True
>>> example_function is other_name_for_example_function
True

Note that in the comparison made at the end, I do not call these functions, I just compare the value of example_function and other_name_for_example_function which in this case is the same "function mechanism".请注意,在最后进行的比较中,我没有调用这些函数,我只是比较了example_functionother_name_for_example_function的值,在这种情况下它们是相同的“函数机制”。

Now, lambdas are another way to define a function but it's more restricted and the function isn't assigned a name automatically.现在,lambdas 是另一种定义 function 的方法,但它受到更多限制,并且 function 不会自动分配名称。 Let's take the same example but with lambdas:让我们举同样的例子,但使用 lambda:

>>> example_lambda = lambda argument: argument * 42
>>> other_name_for_example_lambda = example_lambda
>>> example_lambda(3)
126
>>> other_name_for_example_lambda(3)
126
>>> example_lambda == other_name_for_example_lambda
True
>>> example_lambda is other_name_for_example_lambda
True

Now if we replace the function call of your example with its content, it would look like this:现在,如果我们用其内容替换您示例的 function 调用,它将如下所示:

>>> n = 3   # just you see where that value will be used
>>> mytripler = lambda a: a * n
>>> a = 11  # also to see where that value will be used
>>> mytripler(a)
33

So, in your example,所以,在你的例子中,

  1. myfunc() provide a "function mechanism" as a return value. myfunc()提供“函数机制”作为返回值。 In the definition of that "function mechanism", you have inserted the value 3 which is an argument of myfunc()在该“函数机制”的定义中,您插入了值 3,它是myfunc()的一个参数
  2. You assign the name mytripler to that function returned您将名称mytripler分配给返回的 function
  3. You call it like you would with any function您可以像使用任何 function 一样调用它

Does that help you understand?这有助于你理解吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM