在函数内使用lambda [英] Using lambda inside a function

查看:42
本文介绍了在函数内使用lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过在线教程学习python中的lambda函数.我了解它的工作原理,但遇到了一个使我感到困惑的示例(在此页面上 https://www.w3schools.com/python/python_lambda.asp ):

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 https://www.w3schools.com/python/python_lambda.asp):

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

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

我不明白"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. Thank you.

推荐答案

mydoubler myfunc(2)返回的内容.它返回一个接受单个参数 a 的lambda.

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

当您调用如下函数: myfunction(this_argument)时,它将解析为该位置返回的内容.因此,这实际上与编写 mydoubler = lambda a:a * 2

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

这篇关于在函数内使用lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆