Recursive function always returns None

Erik Max Francis max at alcyone.com
Mon Mar 8 21:32:35 EST 2004


Patrick LeGresley wrote:

> I have a sample piece of code for which I can't understand why it
> returns the output it does.  I am running Python 2.3.2 on Redhat
> Linux 8.0 (x86 hardware).  Here is the code and output:
> 
> granada 6% cat tmp.py
> def test(astring):
> 
>         if (len(astring) >= 30):
>                 print 'astring =',astring
>                 return astring
>         else:
>                 astring = astring + astring
>                 test(astring)

The else branch doesn't return anything, it just recursively calls
itself and disposes of the output.  For this last line, you meant:

	return test(astring)

-- 
 __ Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
/  \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Let us not seek the Republican answer or the Democratic answer but
    the right answer. -- John F. Kennedy



More information about the Python-list mailing list