a dummy python question

rafi rafi at free.fr
Fri Aug 26 00:08:09 CEST 2005


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)



More information about the Python-list mailing list