理解Python中的lambda

摘要: Lambda is another way of creating functions. There's nothing intimidating about it, you're already familiar with functions. The difference is lambda defines functions inline.


先举个例子: (给出一个非负整数,请判定该整数是不是2的n次方。)

理解Python中的lambda_第1张图片
图1

lambda是Python的一个关键字,用来实现一个匿名函数。 将图1的代码转换成函数实现就是:

理解Python中的lambda_第2张图片
图2

如果跟C语言相比较,Python的lambda类似C语言的宏定义。下面上段代码:

理解Python中的lambda_第3张图片
图3
理解Python中的lambda_第4张图片
图4


参考资料:

1. https://www.python-course.eu/lambda.php

2. https://pythonprogramminglanguage.com/lambda/

3. https://stackoverflow.com/questions/890128/why-are-python-lambdas-useful


Oct 16, 2017


你可能感兴趣的:(理解Python中的lambda)