As we all know, if you have a function like this:
def foo():
foo()
The function will call itself 1000 times and then you get a RecursionError
exception.
But what if you had a thousand and one different functions that call each other? i.e. a()
calls b()
calls c()
and so on.
Would you get any sort of error at the 1001st call? The call stack depth would be the same as in the recursive case, even though there isn't any actual recursion.
CodePudding user response:
RecursionError
is thrown when the call stack is exceeded. This most commonly occurs due to recursion, but it can occur in general.
To test this, we can write a quick for
loop to generate a couple hundred different functions that call each other.
function = lambda: "Success!"
for _i in range(999):
def new_function(f=function):
f()
function = new_function
print(function())
At 999, this fails with RecursionError
(the one-thousandth function call is the lambda
, not part of the for loop). Change that loop counter to 998 and it runs fine.