diff options
author | Georg Brandl <georg@python.org> | 2013-10-06 08:22:54 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2013-10-06 08:22:54 (GMT) |
commit | ce28e2c24b4c8bbead80f0ef0e948daff3ba4be6 (patch) | |
tree | 892d95c9dcf2b3226359a0431367314085fc3298 | |
parent | 3db684ff4574d236ec6eea6f5caceff9f886fc92 (diff) | |
parent | de5aff1bdc65a562c15cdd5a661dc666cb25ec16 (diff) | |
download | cpython-ce28e2c24b4c8bbead80f0ef0e948daff3ba4be6.zip cpython-ce28e2c24b4c8bbead80f0ef0e948daff3ba4be6.tar.gz cpython-ce28e2c24b4c8bbead80f0ef0e948daff3ba4be6.tar.bz2 |
merge with 3.3
-rw-r--r-- | Doc/tutorial/controlflow.rst | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/Doc/tutorial/controlflow.rst b/Doc/tutorial/controlflow.rst index 055f547..56df73d 100644 --- a/Doc/tutorial/controlflow.rst +++ b/Doc/tutorial/controlflow.rst @@ -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: |