Update lambda description to reflect nested scopes. This was noted by
Andrew Koenig.
This commit is contained in:
parent
1a76386194
commit
fcf94681ed
|
@ -1535,19 +1535,17 @@ Here's a function that returns the sum of its two arguments:
|
||||||
objects are required. They are syntactically restricted to a single
|
objects are required. They are syntactically restricted to a single
|
||||||
expression. Semantically, they are just syntactic sugar for a normal
|
expression. Semantically, they are just syntactic sugar for a normal
|
||||||
function definition. Like nested function definitions, lambda forms
|
function definition. Like nested function definitions, lambda forms
|
||||||
cannot reference variables from the containing scope, but this can be
|
can reference variables from the containing scope:
|
||||||
overcome through the judicious use of default argument values:
|
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> def make_incrementor(n):
|
>>> def make_incrementor(n):
|
||||||
... return lambda x, incr=n: x+incr
|
... return lambda x: x + n
|
||||||
...
|
...
|
||||||
>>> f = make_incrementor(42)
|
>>> f = make_incrementor(42)
|
||||||
>>> f(0)
|
>>> f(0)
|
||||||
42
|
42
|
||||||
>>> f(1)
|
>>> f(1)
|
||||||
43
|
43
|
||||||
>>>
|
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue