Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
e4feff1d
Commit
e4feff1d
authored
Oct 06, 2013
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Closes #18646: improve lambda docs in tutorial. Original patch by Terry Reedy.
parent
c654a2dc
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
11 deletions
+18
-11
Doc/tutorial/controlflow.rst
Doc/tutorial/controlflow.rst
+18
-11
No files found.
Doc/tutorial/controlflow.rst
View file @
e4feff1d
...
...
@@ -583,17 +583,16 @@ In the same fashion, dictionaries can deliver keyword arguments with the ``**``\
.. _tut-lambda:
Lambda Forms
------------
By popular demand, a few features commonly found in functional programming
languages like Lisp have been added to Python. With the :keyword:`lambda`
keyword, small anonymous functions can be created. Here's a function that
returns the sum of its two arguments: ``lambda a, b: a+b``. Lambda forms can be
used wherever function objects are required. They are syntactically restricted
to a single expression. Semantically, they are just syntactic sugar for a
normal function definition. Like nested function definitions, lambda forms can
reference variables from the containing scope::
Lambda Expressions
------------------
Small anonymous functions can be created with the :keyword:`lambda` keyword.
This function returns the sum of its two arguments: ``lambda a, b: a+b``.
Lambda forms can be used wherever function objects are required. They are
syntactically restricted to a single expression. Semantically, they are just
syntactic sugar for a normal function definition. Like nested function
definitions, lambda functions can reference variables from the containing
scope::
>>> def make_incrementor(n):
... return lambda x: x + n
...
...
@@ -604,6 +603,14 @@ reference variables from the containing scope::
>>> f(1)
43
The above example uses a lambda expression to return a function. Another use
is to pass a small function as an argument::
>>> pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
>>> pairs.sort(key=lambda pair: pair[1])
>>> pairs
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
.. _tut-docstrings:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment