Recursion
is a process in which a function calls itself repeatedly until a certain condition is met. It is implemented by declaring a function that calls itself within its body. For example:
A brief overview of recursion
Recursion is the process of defining a problem in terms of itself. It involves breaking down a problem into smaller and simpler sub-problems and then combining their solutions until a base case is reached.
The base case must always be included in the recursive function definition for the recursion to eventually terminate. For example in our previous example, the base case is reached when the value of 'n' becomes equal to zero.
When a recursive function does not define a base case or the base case is improperly defined, the function will not terminate and will instead enter into what is called an infinite recursion.
When is RecursionError exception raised?
Recursion is a highly resource intensive process. Python in an aim to save memory memory from a recursive call sets a recursion limit. This limit prevents the program from running out of memory, as even a simple recursive function call can quickly use up all of the available memory on a computer. The recursion limit set by Python is by default, 1000, which means that a function can only call itself up to 1000 times before causing an error. Whenever this limit is reached, a RecursionError
is raised.
For example the following function calls itself but does not define a base case leading to the recursion limit being reached and consequently raising the RecursionError
.
Consider the following example:
In the above case, the recursive function works properly with even numbers, this is if you continue subtracting two to an even number, you will eventually reach 0( the base case) thus terminating the recursion. On the case with an odd number, you will never get zero by continuously subtracting two, thus it eventually leads to the recursion limit being exceeded and consequently, RecursionError
is raised. This is a case where the base case is improperly set to handle all the likely inputs.
Avoiding and Handling the RecursionError
The most obvious way to avoid a recursion error is to ensure that the base case is correctly set.
The builtin sys module also offers way to directly increase or decrease the recursion limit. By setting the recursion limit to a value bigger then the default(1000), functions can be able to make more recursive calls.
You should, however, be careful to not set the recursion limit too high, the value is set to 1000 for a good reason. Setting the recursion limit too high can lead to a stack overflow issues which can easily cause your program to crash. As a rule of thumb always try to make sure the recursion limit is below 2000.
We can also use the try-except blocks to catch the RecursionError
exception as it is raised.