Subtitles section Play video
(upbeat music)
(歡快的音樂)
- [Instructor] If you've done any programming
- [指導員]如果你做過任何編程
in other languages, such as JavaScript,
在其他語言中,如JavaScript。
or C#, or Java, you've probably seen
或C#,或Java,你可能已經看到了
or worked with anonymous functions.
或用匿名函數工作。
Python also supports these
Python也支持這些
and they are referred to as lambda functions.
而它們被稱為lambda函數。
Lambda functions can be passed as arguments
Lambda函數可以作為參數傳遞
to other functions to perform some processing work,
到其他功能,以執行一些處理工作。
much like a callback function in a language like JavaScript.
很像JavaScript等語言中的回調函數。
Typically, you see these used in situations
通常情況下,你會看到在以下情況下使用這些東西
where defining a whole separate function
其中定義一個完整的獨立函數
would needlessly increase the complexity of the code
將不必要地增加代碼的複雜性
and reduce readability.
並降低可讀性。
Lambdas are defined by using the keyword lambda,
通過使用關鍵字lambda來定義Lambdas。
followed by any arguments that the lambda function takes
後面是lambda函數的任何參數
and then followed by an expression.
並在後面加上一個表達式。
Let's look at how they are used in practice,
讓我們看看它們在實踐中是如何使用的。
because that's usually the best way to understand something.
因為這通常是理解一件事的最好方法。
Here in VS Code, at the top of the file,
在VS代碼中的這裡,在文件的頂部。
I have two regular functions,
我有兩個常規功能。
each of which performs a conversion
其中每一個都進行了一次轉換
from one temperature scale to another.
從一個溫標到另一個溫標。
This one converts Celsius temperatures to Fahrenheit
這個可以將攝氏溫度轉換為華氏溫度
and this one does the opposite.
而這個人的做法恰恰相反。
In the main function, I have two lists of temperatures;
在主函數中,我有兩個溫度列表。
one in the Celsius scale and one in the Fahrenheit scale.
一個是攝氏刻度,一個是華氏刻度。
Suppose I wanted to convert each of these lists
假設我想轉換這些列表中的每一個
into the other temperature scale.
進入另一個溫標。
To do this, I might use the map function.
要做到這一點,我可能會使用地圖功能。
The map function takes a function
map函數接收一個函數
as the first argument and an iterable object,
作為第一個參數和一個可迭代對象。
like a list, as the second.
像一個列表,作為第二個。
To convert these two lists,
要轉換這兩個列表。
I would write something like map
我會寫一些類似地圖的東西
and then I would call Fahrenheit to Celsius as the function.
然後我將調用華氏到攝氏的函數。
Then I would pass the ftemps list.
然後我就會通過ftemps的名單。
Then to print this out, I'll put this inside a list
然後為了打印出來,我將把這個放在一個列表裡面
to generate the list.
來生成該列表。
Then I'll just call print on the entire thing
然後,我就把整個事情叫作打印。
and I'll copy and paste that,
而我將複製和粘貼這一點。
and do the same thing with Celsius to Fahrenheit
並做同樣的事情,將攝氏度改為華氏度
and pass in the ctemps.
並在ctemps中通過。
Let's run what we have.
讓我們運行我們擁有的東西。
I'll go to debug view.
我轉到調試視圖。
Remember, you can run from the Terminal command-line
記住,你可以從終端命令行運行
if you don't want to use VS Code.
如果你不想使用VS代碼。
I'll run this.
我將運行這個。
You can see the results here
你可以在這裡看到結果
that each of the temperatures has been converted.
每個溫度都已轉換。
I could just reduce the complexity of my code
我可以只減少我的代碼的複雜性
by writing each of these functions
通過編寫這些函數中的每一個
as an in-line lambda, because they're pretty simple.
作為一個內聯lambda,因為它們非常簡單。
Let's go back and do that.
讓我們回去做這件事。
I'll clear the console.
我將清除控制檯。
I'm going to copy these two lines
我將複製這兩行
and paste them down here.
並把它們粘貼到這裡。
Now I'll replace each function with a lambda equivalent.
現在我將用一個lambda等價物來替換每個函數。
I'll put in lambda T, because each lambda function
我把lambda T放進去,因為每個lambda函數
will take a temperature as an argument.
將接受一個溫度作為參數。
Then for the Fahrenheit to Celsius case,
然後對於華氏到攝氏的情況。
I will copy this expression
我將複製這個表達方式
and paste it in here.
並將其粘貼在這裡。
I change that to T 'cause it's not temp anymore.
我把它改為T,因為它不再是臨時性的了。
The same thing here.
這裡也是如此。
I'll write lambda T,
我就寫lambda T。
and now I will get the Celsius to Fahrenheit version.
而現在我將得到攝氏度到華氏度的版本。
I'll copy that, paste it in,
我把它複製下來,粘貼進去。
and change that to T.
並將其改為T。
Now you can see that the results are the same.
現在你可以看到,結果是一樣的。
In this particular case,
在這個特殊的情況下。
using the lambda expression really simplifies my code
使用lambda表達式確實簡化了我的代碼
because I can see the calculation
因為我可以看到計算結果
right where it's being used.
就在它被使用的地方。
Someone else who has to work with my code,
其他必須與我的代碼一起工作的人。
even if that's me several years from now,
即使那是若干年後的我。
doesn't have to go digging through all the source code
不需要去挖掘所有的源代碼
to find out where the conversion functions are defined.
來找出轉換函數的定義。
Obviously, lambdas aren't a good fit for every scenario.
很明顯,lambdas並不適合每一種情況。
In practice, you will, of course,
在實踐中,你當然會。
continue to use regular functions in your programs,
繼續在你的程序中使用常規函數。
but lambdas can help make your code more readable
但lambdas可以幫助你的代碼更加可讀
when defining a full function
當定義一個完整的函數時
is more effort than it's worth.
是事倍功半。
(upbeat music)
(歡快的音樂)