diff options
author | Fred Drake <fdrake@acm.org> | 2001-06-05 02:24:26 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2001-06-05 02:24:26 (GMT) |
commit | 05f29200de144ff5d4ea2027df370a7b36b666d7 (patch) | |
tree | 7e22b806506bbd2e378ac6f1748dba224484ac79 /Doc/ref/ref5.tex | |
parent | 35a37f1c013756f67d54c28cd4b827957b4c4687 (diff) | |
download | cpython-05f29200de144ff5d4ea2027df370a7b36b666d7.zip cpython-05f29200de144ff5d4ea2027df370a7b36b666d7.tar.gz cpython-05f29200de144ff5d4ea2027df370a7b36b666d7.tar.bz2 |
Update a "Programmer's note" about lambda forms and scoping to reflect
the availability of nested scoping in Python 2.1.
Note that this is a slightly different patch than was applied to the trunk
of the development for Python 2.2.
Diffstat (limited to 'Doc/ref/ref5.tex')
-rw-r--r-- | Doc/ref/ref5.tex | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/Doc/ref/ref5.tex b/Doc/ref/ref5.tex index f2a1d1b..b9eaad2 100644 --- a/Doc/ref/ref5.tex +++ b/Doc/ref/ref5.tex @@ -867,17 +867,31 @@ that functions created with lambda forms cannot contain statements. \indexii{lambda}{form} \indexii{anonmymous}{function} -\strong{Programmer's note:} a lambda form defined inside a function -has no access to names defined in the function's namespace. This is -because Python has only two scopes: local and global. A common -work-around is to use default argument values to pass selected -variables into the lambda's namespace, e.g.: +\strong{Programmer's note:} Prior to Python 2.1, a lambda form defined +inside a function has no access to names defined in the function's +namespace. This is because Python had only two scopes: local and +global. A common work-around was to use default argument values to +pass selected variables into the lambda's namespace, e.g.: \begin{verbatim} def make_incrementor(increment): return lambda x, n=increment: x+n \end{verbatim} +Python 2.1 introduced nested scopes as an optional feature, and this +work-around has not been necessary when the feature is enabled. The +use of nested scopes is enabled by the statement \samp{from __future__ +import nested_scopes}; future versions of Python will enable nested +scopes by default. This version works starting with Python 2.1: + +\begin{verbatim} +from __future__ import nested_scopes + +def make_incrementor(increment): + return lambda x: x+increment +\end{verbatim} + + \section{Expression lists\label{exprlists}} \indexii{expression}{list} |