[Python-checkins] CVS: python/dist/src/Doc/ref ref5.tex,1.45,1.46

Fred L. Drake fdrake@users.sourceforge.net
Mon, 04 Jun 2001 19:17:04 -0700


Update of /cvsroot/python/python/dist/src/Doc/ref
In directory usw-pr-cvs1:/tmp/cvs-serv6705/ref

Modified Files:
	ref5.tex 
Log Message:

Update a "Programmer's note" about lambda forms and scoping to reflect
the availability of nested scoping in Python 2.1 and 2.2.


Index: ref5.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/ref/ref5.tex,v
retrieving revision 1.45
retrieving revision 1.46
diff -C2 -r1.45 -r1.46
*** ref5.tex	2001/05/09 16:51:49	1.45
--- ref5.tex	2001/06/05 02:17:02	1.46
***************
*** 870,878 ****
  \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.:
  
  \begin{verbatim}
--- 870,878 ----
  \indexii{anonmymous}{function}
  
! \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}
***************
*** 880,883 ****
--- 880,897 ----
      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}}