Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis+ 2
What would be a good reason to use Lambdas as a normal function?
example from lesson: double = lambda x: x * 2 print(double(7)) output 14
2 Respostas
+ 6
The syntax is more concise in certain situations, mostly when dealing withĀ mapet al.
ex:-
map(lambda x: x * 2, [1,2,3,4])
seems better to me than:
def double(x):
return x * 2 map(double, [1,2,3,4])
I think the lambda is a better choice in this situation because theĀ def double seems almost disconnected from theĀ mapthat is using it. Plus, I guess it has the added benefit that the function gets thrown away when you are done.
There is one downside to lambda which limits its usefulness in Python, in my opinion: lambdas can have only one expression (i.e., you can't have multiple lines). It just can't work in a language that forces whitespace.
0
In C# I use lambdas a lot for simply calling functions with different but similar parameters than the delegate I need to pass.
myVar.SetLogFunction(message => log.Write(LogStatus.Info, message));