Home >Backend Development >Python Tutorial >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!