[Tutor] My first recursive function...

Andrei project5 at redrival.net
Thu Jul 28 13:28:40 CEST 2005


Liam Clarke <cyresse <at> gmail.com> writes:

> Are there any other pitfalls with recursion I need to watch for? 

Not just infinite recursion, but recursion that exceeds the maximum recursion
depth (1000 on my system). You can add a counter parameter to your recursive
function and stop recursing if the function finds it has called itself too many
times - helps against infinite recursion too.

>>> def a(i):
...     i += 1
...     if i < 500:
...         a(i)
>>> a(0)

If you try the function above without the if condition, it will generate a
RuntimeError.  

Yours,

Andrei



More information about the Tutor mailing list