merge with 3.3
This commit is contained in:
commit
ce28e2c24b
@ -583,17 +583,16 @@ In the same fashion, dictionaries can deliver keyword arguments with the ``**``\
|
|||||||
|
|
||||||
.. _tut-lambda:
|
.. _tut-lambda:
|
||||||
|
|
||||||
Lambda Forms
|
Lambda Expressions
|
||||||
------------
|
------------------
|
||||||
|
|
||||||
By popular demand, a few features commonly found in functional programming
|
Small anonymous functions can be created with the :keyword:`lambda` keyword.
|
||||||
languages like Lisp have been added to Python. With the :keyword:`lambda`
|
This function returns the sum of its two arguments: ``lambda a, b: a+b``.
|
||||||
keyword, small anonymous functions can be created. Here's a function that
|
Lambda forms can be used wherever function objects are required. They are
|
||||||
returns the sum of its two arguments: ``lambda a, b: a+b``. Lambda forms can be
|
syntactically restricted to a single expression. Semantically, they are just
|
||||||
used wherever function objects are required. They are syntactically restricted
|
syntactic sugar for a normal function definition. Like nested function
|
||||||
to a single expression. Semantically, they are just syntactic sugar for a
|
definitions, lambda functions can reference variables from the containing
|
||||||
normal function definition. Like nested function definitions, lambda forms can
|
scope::
|
||||||
reference variables from the containing scope::
|
|
||||||
|
|
||||||
>>> def make_incrementor(n):
|
>>> def make_incrementor(n):
|
||||||
... return lambda x: x + n
|
... return lambda x: x + n
|
||||||
@ -604,6 +603,14 @@ reference variables from the containing scope::
|
|||||||
>>> f(1)
|
>>> f(1)
|
||||||
43
|
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:
|
.. _tut-docstrings:
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user