"lambda"到底是什么?在Python中? [英] What exactly is "lambda" in Python?

查看:61
本文介绍了"lambda"到底是什么?在Python中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道python中的lambda到底是什么?以及在何处以及为何使用它. 谢谢

I want to know what exactly is lambda in python? and where and why it is used. thanks

推荐答案

Lambda与其说是其他概念,不如说是一种概念或编程技术.

Lambda is more of a concept or programming technique then anything else.

基本上是这样的想法,即您获得了另一个函数(而不是对象或原始类型)的结果而返回的函数(python中的一流对象).我知道,这很令人困惑.

Basically it's the idea that you get a function (a first-class object in python) returned as a result of another function instead of an object or primitive type. I know, it's confusing.

请参见 Python文档:

def make_incrementor(n):
  return lambda x: x + n
f = make_incrementor(42)
f(0)
>>> 42
f(1)
>>> 43

因此make_incrementor创建一个在结果中使用n的函数.您可以使用将参数增加2的函数,如下所示:

So make_incrementor creates a function that uses n in it's results. You could have a function that would increment a parameter by 2 like so:

f2 = make_incrementor(2)
f2(3)
>>> 5

这在函数式编程和函数式编程语言(如lisp&方案.

This is a very powerful idea in functional programming and functional programming languages like lisp & scheme.

希望这会有所帮助.

这篇关于"lambda"到底是什么?在Python中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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