Home  >  Article  >  Backend Development  >  What is lambda in python?

What is lambda in python?

藏色散人
藏色散人Original
2019-06-22 11:33:367971browse

What is lambda in python?

Python uses lambda to create anonymous functions. Lambda is just an expression, and the function body is much simpler than def. The body of a lambda is an expression, not a block of code. Only limited logic can be encapsulated in lambda expressions. The lambda function has its own namespace and cannot access parameters outside its own parameter list or in the global namespace.

Although the lambda function seems to only be able to write one line, it is not equivalent to the inline function of C or C. The purpose of the latter is to not occupy the stack memory when calling a small function and thus increase the operating efficiency.

Lambda simplifies the writing form of functions and makes the code more concise.

func = lambda x : x * x

x corresponds to the entry parameter of the function, x * x corresponds to the function body

The above lambda expression is equivalent to

def func(x):
    return x * x
print func(2)

The result obtained is 4

Related recommendations: "Python Tutorial"

The above is the detailed content of What is lambda in python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is sorted in pythonNext article:What is sorted in python