Re: a dummy python question

2005-08-26 Thread 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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a dummy python question

2005-08-25 Thread Learning Python
Thanks all for replying.
I finally know what's going on.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a dummy python question

2005-08-25 Thread Robert Kern
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
[EMAIL PROTECTED]

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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a dummy python question

2005-08-25 Thread rafi
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)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a dummy python question

2005-08-25 Thread [EMAIL PROTECTED]
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 =

[EMAIL PROTECTED] ~]$ ./scope_test.py
3 2 1 0

[EMAIL PROTECTED] ~]$ 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a dummy python question

2005-08-25 Thread infidel

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!

-- 
http://mail.python.org/mailman/listinfo/python-list


a dummy python question

2005-08-25 Thread Learning Python
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

-- 
http://mail.python.org/mailman/listinfo/python-list