Difference between revisions of "Lambda function"

From CS 61A Wiki
Jump to: navigation, search
[checked revision][checked revision]
(Added a formal definition for lambda. Add the lexical scoping tidbit when the class is ready.)
(Added Basics sidebar)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{Sufficient-class}}
 
{{Sufficient-class}}
A '''lambda function''' is an anonymous function; that is, it has no intrinsic name. It is a compact way to define a function inline without using the <tt>def</tt> statement. Use a lambda function if its body is a simple expression.  
+
{{Basics sidebar}}
 +
A '''lambda function''' is an anonymous [[function]]; that is, it has no intrinsic name. It is a compact way to define a function inline without using the <tt>def</tt> [[Statement#Function_definition| statement]]. Use a lambda function if its body is a single [[expression]].
 +
 
 +
Lambda functions can be used wherever function objects are required.
  
 
== Definition ==
 
== Definition ==
Line 6: Line 9:
 
lambda args: return_expression
 
lambda args: return_expression
 
</syntaxhighlight>
 
</syntaxhighlight>
The lambda expression above in Python behaves like the code below.   
+
The lambda expression above in Python is roughly equivalent to the function defined below.   
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
def <lambda>(args):
 
def <lambda>(args):
 
     return return_expression  
 
     return return_expression  
 
</syntaxhighlight>
 
</syntaxhighlight>
Note that unlike with <code>def</code>, the lambda function does not automatically get bounded to a name in the [[Frame|frame]] (hence its anonymity). In an [[Environment diagram|environment diagram]], a new function is created in the heap when the lambda expression gets evaluated.  
+
Note that unlike with <code>def</code>, the lambda function does not automatically get bound to a name in the [[frame]] (hence its anonymity). In an [[environment diagram]], a new function object is created in the heap when the lambda expression gets evaluated.
 
+
 
<!-- A lambda function is lexically scoped, so the new function object's parent is the current frame, the frame in which the lambda expression is evaluated, not the frame where any subsequent call expressions gets evaluated. -->
 
<!-- A lambda function is lexically scoped, so the new function object's parent is the current frame, the frame in which the lambda expression is evaluated, not the frame where any subsequent call expressions gets evaluated. -->
 +
 
== Examples ==
 
== Examples ==
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">

Latest revision as of 11:02, 9 July 2014

A lambda function is an anonymous function; that is, it has no intrinsic name. It is a compact way to define a function inline without using the def statement. Use a lambda function if its body is a single expression.

Lambda functions can be used wherever function objects are required.

Definition

lambda args: return_expression

The lambda expression above in Python is roughly equivalent to the function defined below.

def <lambda>(args):
    return return_expression

Note that unlike with def, the lambda function does not automatically get bound to a name in the frame (hence its anonymity). In an environment diagram, a new function object is created in the heap when the lambda expression gets evaluated.

Examples

>>> def call_f(f):
...     return f()
... 
>>> call_f(lambda: 3)
3

You can bind a variable name to a lambda function if you want to refer to it in the future:

>>> even = lambda x: x % 2 == 0
>>> even(40)
True
>>> even(5)
False

You can also call lambda functions like regular functions:

>>> (lambda x: x % 2 == 0)(40)
True

External links