recursive function return value problems

Steve Holden steve at holdenweb.com
Thu Dec 29 04:21:22 CET 2005


randomtalk at gmail.com wrote:
> hi, i have the following recursive function (simplified to demonstrate
> the problem):
> 
> 
>>>>def reTest(bool):
> 
> ... 	result = []
> ... 	if not bool:
> ... 		reTest(True)
> ... 	else:
> ... 		print "YAHHH"
> ... 		result = ["should be the only thing returned"]
> ... 	print "printing result: "
> ... 	print result
> ... 	return result
> ...
> 
>>>>reTest(False)
> 
> YAHHH
> printing result:
> ['should be the only thing returned']
> printing result:
> []
> []
> 
> I don't understand why results are returned twice? is there something
> special i missed about recursive functions?
> 
The only thing you appear to have misunderstood is that there;s 
absolutely nothing special about recursive functions at all, and the 
Python interpreter is doing exactly what you told it to.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list