By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,824 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

a dummy python question

P: n/a
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.

but I typed in this in python interface. It works!
it print out:
3 2 1 0
If you turn this into a module file and run this
it print out
3 2 1 0 none

Can anyone explain to me what's going on?

Thanks

BTW: I am using Python 2.3

Aug 25 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

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!

Aug 25 '05 #2

P: n/a
This is not reproducible under either Python 2.3.4 (UNIX), Python 2.4.1
(UNIX) or Python 2.4.1 (Windows). If you still need help, we need to
know precisely what you're doing.

===== scope_test.py =====
#!/usr/bin/env python
#
# (insert his code, verbatim...)
#
if __name__=='__main__':
outer(3)

===== end scope_test.py =====

[rjhansen@serv16 ~]$ ./scope_test.py
3 2 1 0

[rjhansen@serv16 ~]$ python
Python 2.3.4 (#1, Feb 2 2005, 11:44:13)
[GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
from scope_test import outer
outer(3)

3 2 1 0

Aug 25 '05 #3

P: n/a
Learning Python wrote:
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.


There is no error. the function inner is defined recursively: It calls
itself with a different value than the one it has been called with. When
defining a recursive function, there are case when it calls itself and
other when it does not (otherwise the recursion is infinite and the
program crashes after all the memory is used). Here it does not call
itself when the value given as parameter is 0 (the if fails).

one can always see itself (even at definition time)
but I typed in this in python interface. It works!
it print out:
3 2 1 0

If you turn this into a module file and run this
it print out
3 2 1 0 none


I suppose you wrote this down (instead of cut and paste) as the none is
not capitalized. There must be something else in your module that writes
the none as your code presented above should really not "as is".

--
rafi

"Imagination is more important than knowledge."
(Albert Einstein)
Aug 25 '05 #4

P: n/a
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/what...hatsnew22.html
http://www.python.org/doc/2.3.5/what...hatsnew23.html
http://www.python.org/doc/2.4.1/what...hatsnew24.html

--
Robert Kern
rk***@ucsd.edu

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

Aug 25 '05 #5

P: n/a
Thanks all for replying.
I finally know what's going on.

Aug 25 '05 #6

P: n/a
> > 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.


Ah, that makes sense. Thanks for the clarification.

Aug 26 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.