What does "lambda" mean in Python, and what's the simplest way to use it? -


Can you give an example and other examples when and when do not use Lambda? My book gives me examples, but they are confused.

Lambda, originated from and (AFAIK) was implemented in the first place, basically There is an anonymous function - a function in which there is no name, and in other words in-line is used, you can assign an expression to an identifier in an expression like:

< Previous> & gt; & Gt; & Gt; AddTwo = Lambda X: x + 2> gt; & Gt; & Gt; AddTwo (2) 4

This provides the anonymous code to addTwo , which accepts 1 argument x, and in the function body it adds X to 2 , Returns this function, the last value of the last expression in the function body is therefore no return keyword.

The above code is almost equivalent:

  & gt; & Gt; & Gt; Def addTwo (x): ... back to x + 2 ... & gt; & Gt; & Gt; AddTwo (2) 4  

Except you are not using a function definition, you are telling Lambda an identifier.

The best place to use them is when you do not really want to define a function with a name, possibly because that function is used only a time and many times In this situation, you will be better with the function definition

Example of a hash tree using Lambda:

  & gt; & Gt; & Gt; MapTree = {... 'number': lambda x: x ** x, ... 'string': lambda x: x [1:] ...}> gt; & Gt; & Gt; Otype = 'number' & gt; & Gt; & Gt; MapTree [otype] (2) 4 & gt; & Gt; & Gt; Otype = 'string' & gt; & Gt; & Gt; In this example, I really do not want to define any one of those functions because I will only use them within them, Matri [opte] ('foo') 'oo'  
< P> hash, so I will use lambdass.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -