博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python lambda
阅读量:4030 次
发布时间:2019-05-24

本文共 4773 字,大约阅读时间需要 15 分钟。

To understand the lambda keyword/function and their constructs we must first review how regular function definitions work in Python. The following is the basic structure of a function definition:

What we've defined here is essentially an empty function named basic, which receives one argument, does nothing (the pass statement is essentially a valid "do nothing" statement), and then returns the argument it was given. This is the most basic structure of most functions, but the simplest function definition would only include one of these statements - in one statement it would either do something (or pass), or return something or None (a return statement always returns at least the None instance, so it can't actually return nothing). Here is how these two cases would look:

Notice how we also saved a line by putting the function body statements on the same line as the definition statement. A lambda function structure is exactly the latter of these two structures, i.e. a one-statement function which returns something, the difference being that the "return" keyword is implied and doesn't show up in the construct. Now that we know how lambda functions work (identically to a one-statement function), let's construct the simple2 function in lambda form. To do this, we simply use the lambda construct - if we call our lambda function simplest, it would be like so:

And that's it! Now we have simplest and simple2 which behave the exact same way, but the former we constructed with much less jargon; however, technically speaking we only saved two characters in this case, so what's the big deal?

If we look in detail at the lambda statement we find that it is actually a statement that returns a function (which is why we assign it to a variable), and this is where the real power of the lambda construct is, so let's look at an example where we harness this power.

Let's say we have a list of 3-tuples which looks like this:

If we want a sorted version of tups we'd simply have to give it to the built-in sorted method:

This is fine if we only want to sort by the first value in each tuple, but what if we want to sort by the second or third value? Knowing how the built-in sorted method works, we can use the key argument to define a function that will return the desired value (see  for some background on sorted). Here's how it would look for sorting with the 2nd value in each tuple:

This works perfectly, but we've used up extra space/lines (if in a script file), and extra memory for a one-statement function that only helps to sort our list. If using a different language, we might have no choice but to do this (or worse), but fortunately we have the lambda function construct :-). With it we can define the same function in a simpler form:

In the case of simplest we assigned the lambda function returned by the lambda construct/statement to a value, but the power of the lambda construct is we don't have to do that. Instead we can simply use the statement directly in sorted for the key:

It works the same way as with sortkey, but by using this form the process is simpler and saves lines and memory!

This is the power of lambda functions and their construct, but the best part is it will also work for any part of your script that needs such a function. That includes the built-in sorting/iterating functions (e.g. sortedmap, and filter), class methods such as re.sub (with the repl argument as a function), instance methods (as with list.sort), callback functions for Tkinter widgets, etc. So don't hesitate to put lambda functions to work and explore all that they are capable of doing!

About The Author

转载地址:http://vohbi.baihongyu.com/

你可能感兴趣的文章
pyQt不同窗体间的值传递(一)——对话框关闭时返回值给主窗口
查看>>
linux mint下使用外部SMTP(如网易yeah.net)发邮件
查看>>
北京联通华为光猫HG8346R破解改桥接
查看>>
python使用win32*模块模拟人工操作——城通网盘下载器(一)
查看>>
python append 与浅拷贝
查看>>
Matlab与CUDA C的混合编程配置出现的问题及解决方案
查看>>
2017阿里内推笔试题--算法工程师(运筹优化)
查看>>
python自动化工具之pywinauto(零)
查看>>
python一句话之利用文件对话框获取文件路径
查看>>
PaperDownloader——文献命名6起来
查看>>
PaperDownloader 1.5.1——更加人性化的文献下载命名解决方案
查看>>
如何将PaperDownloader下载的文献存放到任意位置
查看>>
C/C++中关于动态生成一维数组和二维数组的学习
查看>>
系统架构:Web应用架构的新趋势---前端和后端分离的一点想法
查看>>
JVM最简生存指南
查看>>
漂亮的代码,糟糕的行为——解决Java运行时的内存问题
查看>>
Java的对象驻留
查看>>
自己动手写GC
查看>>
Java 8新特性终极指南
查看>>
logback高级特性使用(二) 自定义Pattern模板
查看>>