a dummy python question

Robert Kern rkern at ucsd.edu
Fri Aug 26 00:07:52 CEST 2005


infidel wrote:
> Learning Python wrote:
> 
>>A example in learning Python by Mark Lutz and David Ascher
>>
>>about function scope
>>
>>example like this:
>>
>>
>>>>def outer(x):
>>
>>     def inner(i):
>>        print i,
>>        if i: inner(i-1)
>>     inner(x)
>>
>>>>outer(3)
>>
>>Here supposely, it should report error, because the function inner
>>cannot see itself since inner is only in local namespace of outer.
> 
> If that were so, Pythonistas could never write a recursive function!

No, presumably at the writing of the edition of _Learning Python_ that
he is reading, Python did not have nested scopes in the language, yet.
One could always write a recursive function provided it was at the
top-level of the module. One could not write a recursive function inside
another function because inside inner(), it could only access two
namespaces, the one local to inner() and the module's namespace, not the
namespace of outer() where inner() is defined.

For the original poster: Your book is old. You will want to catch up on
recent additions to the language by reading the "What's New in Python
2.x" portions of the documentation for each major revision. Specifically:

http://www.python.org/doc/2.2.3/whatsnew/node9.html

http://www.python.org/doc/2.2.3/whatsnew/whatsnew22.html
http://www.python.org/doc/2.3.5/whatsnew/whatsnew23.html
http://www.python.org/doc/2.4.1/whatsnew/whatsnew24.html

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter




More information about the Python-list mailing list