Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8838269

Browse files
committed
Update a "Programmer's note" about lambda forms and scoping to reflect
the availability of nested scoping in Python 2.1 and 2.2.
1 parent d993c87 commit 8838269

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

Doc/ref/ref5.tex

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -869,17 +869,31 @@ \section{Boolean operations\label{Booleans}}
869869
\indexii{lambda}{form}
870870
\indexii{anonmymous}{function}
871871

872-
\strong{Programmer's note:} a lambda form defined inside a function
873-
has no access to names defined in the function's namespace. This is
874-
because Python has only two scopes: local and global. A common
875-
work-around is to use default argument values to pass selected
876-
variables into the lambda's namespace, e.g.:
872+
\strong{Programmer's note:} Prior to Python 2.1, a lambda form defined
873+
inside a function has no access to names defined in the function's
874+
namespace. This is because Python had only two scopes: local and
875+
global. A common work-around was to use default argument values to
876+
pass selected variables into the lambda's namespace, e.g.:
877877

878878
\begin{verbatim}
879879
def make_incrementor(increment):
880880
return lambda x, n=increment: x+n
881881
\end{verbatim}
882882

883+
As of Python 2.1, nested scopes were introduced, and this work-around
884+
has not been necessary. Python 2.1 supports nested scopes in modules
885+
which include the statement \samp{from __future__ import
886+
nested_scopes}, and more recent versions of Python enable nested
887+
scopes by default. This version works starting with Python 2.1:
888+
889+
\begin{verbatim}
890+
from __future__ import nested_scopes
891+
892+
def make_incrementor(increment):
893+
return lambda x: x+increment
894+
\end{verbatim}
895+
896+
883897
\section{Expression lists\label{exprlists}}
884898
\indexii{expression}{list}
885899

0 commit comments

Comments
 (0)