Python Functions: Lambda Functions

Translation Notice
This article was machine-translated using DeepSeek-R1.

  • Original Version: Authored in Chinese by myself
  • Accuracy Advisory: Potential discrepancies may exist between translations
  • Precedence: The Chinese text shall prevail in case of ambiguity
  • Feedback: Technical suggestions regarding translation quality are welcomed

Note: If the reader hasn’t learned about functions defined with def, please refer to here first

Definition Syntax

1
<function_name> = lambda <parameters>: <return_value>

Equivalent to:

1
2
def <function_name>(<parameters>):
    return <return_value>

Can also be defined as anonymous function (nameless function):

1
lambda <parameters>: <return_value>

We can confirm that lambda function objects have the same type (function) as those defined with def:

Invocation

For anonymous functions, the invocation form is:

1
(lambda <parameters>: <return_value>)(<arguments>)

For named functions:

1
<function_name>(<arguments>)

Function to calculate $a+b$:
a+b_img

No Return Value

lambda functions can also have no return value.
Example:

Lambda functions without return value are generally used for performing actions, such as print('Hello World') etc.

Built with Hugo
Theme Stack designed by Jimmy