summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2001-06-05 02:17:02 (GMT)
committerFred Drake <fdrake@acm.org>2001-06-05 02:17:02 (GMT)
commit88382696f48244e8dbb30cfcf1882b57cb019fc0 (patch)
treea89badc6750d13ca499a77383ad9879b3c8a9ccb /Doc
parentd993c87918a8a6d4f62b24565b825ff4ed0bfad1 (diff)
downloadcpython-88382696f48244e8dbb30cfcf1882b57cb019fc0.zip
cpython-88382696f48244e8dbb30cfcf1882b57cb019fc0.tar.gz
cpython-88382696f48244e8dbb30cfcf1882b57cb019fc0.tar.bz2
Update a "Programmer's note" about lambda forms and scoping to reflect
the availability of nested scoping in Python 2.1 and 2.2.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/ref/ref5.tex24
1 files changed, 19 insertions, 5 deletions
diff --git a/Doc/ref/ref5.tex b/Doc/ref/ref5.tex
index 9590ee4..d66996a 100644
--- a/Doc/ref/ref5.tex
+++ b/Doc/ref/ref5.tex
@@ -869,17 +869,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}
+As of Python 2.1, nested scopes were introduced, and this work-around
+has not been necessary. Python 2.1 supports nested scopes in modules
+which include the statement \samp{from __future__ import
+nested_scopes}, and more recent versions of Python 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}