a dummy python question

L

Learning Python

A example in learning Python by Mark Lutz and David Ascher

about function scope

example like this:
def inner(i):
print i,
if i: inner(i-1)
inner(x)
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
 
I

infidel

Learning said:
A example in learning Python by Mark Lutz and David Ascher

about function scope

example like this:

def inner(i):
print i,
if i: inner(i-1)
inner(x)

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!
 
C

cipherpunk

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.3 2 1 0
 
R

Robert Kern

infidel said:
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
(e-mail address removed)

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

rafi

Learning said:
def inner(i):
print i,
if i: inner(i-1)
inner(x)


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".
 
I

infidel

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top