473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

a dummy python question

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
6 1449

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
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
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
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
Thanks all for replying.
I finally know what's going on.

Aug 25 '05 #6
> > 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jonathan Fine | last post by:
I'd appreciate comments on what I'm doing. Please be kind, I'm a new kid on the block. Here's my application. I'm writing some test code. (I'm a recent convert to unit testing.)
4
by: wireless | last post by:
I've written code that dynamically builds an sql query based on various constraint possibilities. The problem is the code would have been very complex had I not come up with a dummy constraint...
15
by: glenn | last post by:
Hi folks, I have a DropDownList in a DataGrid that is populated from records in a database. I want to add a value that might be a string such as "Select a Company" for the first item since an...
2
by: Dongsheng Ruan | last post by:
I remember that in python there is some kind of dummy statement that just holds space and does nothing. I want it to hold the place after a something like if a>b: do nothing I can't just leave...
3
by: Simon Brooke | last post by:
As various people will have noticed, I've been having a lot of trouble with XSL lately. Brief history: I wrote myself an XML toolkit back in 2000, and it worked well enough for me, so it's been...
1
by: tleeuwenburg | last post by:
Hi all, I remember a tool which (basically) creates a directory somewhere in the user's home directory and puts a copy (or perhaps link to) the python interpreter, adding that directory to the...
35
by: erik gartz | last post by:
Hi. I'd like to be able to write a loop such as: for i in range(10): pass but without the i variable. The reason for this is I'm using pylint and it complains about the unused variable i. I can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.