理解Python中的lambda
2017-10-16 本文已影响0人
idorax
摘要: 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次方。)
图1
lambda是Python的一个关键字,用来实现一个匿名函数。 将图1的代码转换成函数实现就是:
图2
如果跟C语言相比较,Python的lambda类似C语言的宏定义。下面上段代码:
图3
图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