Recursionerror maximum recursion depth exceeded ошибка

I have the following recursion code, at each node I call sql query to get the nodes belong to the parent node.

here is the error:

Exception RuntimeError: 'maximum recursion depth exceeded' in <bound method DictCursor.__del__ of <MySQLdb.cursors.DictCursor object at 0x879768c>> ignored

RuntimeError: maximum recursion depth exceeded while calling a Python object
Exception AttributeError: "'DictCursor' object has no attribute 'connection'" in <bound method DictCursor.__del__ of <MySQLdb.cursors.DictCursor object at 0x879776c>> ignored

Method that I call to get sql results:

def returnCategoryQuery(query, variables={}):
    cursor = db.cursor(cursors.DictCursor);
    catResults = [];
    try:
        cursor.execute(query, variables);
        for categoryRow in cursor.fetchall():
            catResults.append(categoryRow['cl_to']);
        return catResults;
    except Exception, e:
        traceback.print_exc();

I actually don’t have any issue with the above method but I put it anyways to give proper overview of the question.

Recursion Code:

def leaves(first, path=[]):
    if first:
        for elem in first:
            if elem.lower() != 'someString'.lower():
                if elem not in path:
                    queryVariable = {'title': elem}
                    for sublist in leaves(returnCategoryQuery(categoryQuery, variables=queryVariable)):
                        path.append(sublist)
                        yield sublist
                    yield elem

Calling the recursive function

for key, value in idTitleDictionary.iteritems():
    for startCategory in value[0]:
        print startCategory + " ==== Start Category";
        categoryResults = [];
        try:
            categoryRow = "";
            baseCategoryTree[startCategory] = [];
            #print categoryQuery % {'title': startCategory};
            cursor.execute(categoryQuery, {'title': startCategory});
            done = False;
            while not done:
                categoryRow = cursor.fetchone();
                if not categoryRow:
                    done = True;
                    continue;
                rowValue = categoryRow['cl_to'];
                categoryResults.append(rowValue);
        except Exception, e:
            traceback.print_exc();
        try:
            print "Printing depth " + str(depth);
            baseCategoryTree[startCategory].append(leaves(categoryResults))
        except Exception, e:
            traceback.print_exc();

Code to print the dictionary,

print "---Printing-------"
for key, value in baseCategoryTree.iteritems():
    print key,
    for elem in value[0]:
        print elem + ',';
    raw_input("Press Enter to continue...")
    print

If the recursion is too deep I should be getting the error when I call my recursion function, but when I get this error when I print the dictionary.

You might have seen a Python recursion error when running your Python code. Why does this happen? Is there a way to fix this error?

A Python RecursionError exception is raised when the execution of your program exceeds the recursion limit of the Python interpreter. Two ways to address this exception are increasing the Python recursion limit or refactoring your code using iteration instead of recursion.

Let’s go through some examples so you can understand how this works.

The recursion begins!

Let’s create a program to calculate the factorial of a number following the formula below:

n! = n * (n-1) * (n-2) * ... * 1

Write a function called factorial and then use print statements to print the value of the factorial for a few numbers.

def factorial(n):
    if n == 0:
        return 1
    else:
        return n*factorial(n-1) 

This is a recursive function…

A recursive function is a function that calls itself. Recursion is not specific to Python, it’s a concept common to most programming languages.

You can see that in the else statement of the if else we call the factorial function passing n-1 as parameter.

The execution of the function continues until n is equal to 0.

Let’s see what happens when we calculate the factorial for two small numbers:

if __name__ == '__main__': 
    print("The factorial of 4 is: {}".format(factorial(4)))
    print("The factorial of 5 is: {}".format(factorial(5)))

[output]
The factorial of 4 is: 24
The factorial of 5 is: 120 

After checking that __name__ is equal to ‘__main__’ we print the factorial for two numbers.

It’s all good.

But, here is what happens if we calculate the factorial of 1000…

print("The factorial of 1000 is: {}".format(factorial(1000)))

[output]
Traceback (most recent call last):
  File "recursion_error.py", line 9, in <module>
    print("The factorial of 1000 is: {}".format(factorial(1000)))
  File "recursion_error.py", line 5, in factorial
    return n*factorial(n-1)
  File "recursion_error.py", line 5, in factorial
    return n*factorial(n-1)
  File "recursion_error.py", line 5, in factorial
    return n*factorial(n-1)
  [Previous line repeated 995 more times]
  File "recursion_error.py", line 2, in factorial
    if n <= 1:
RecursionError: maximum recursion depth exceeded in comparison 

The RecursionError occurs because the Python interpreter has exceeded the recursion limit allowed.

The reason why the Python interpreter limits the number of times recursion can be performed is to avoid infinite recursion and hence avoid a stack overflow.

Let’s have a look at how to find out what the recursion limit is in Python and how to update it.

What is the Recursion Limit in Python?

Open the Python shell and use the following code to see the value of the recursion limit for the Python interpreter:

>>> import sys
>>> print(sys.getrecursionlimit())
1000 

Interesting…the limit is 1000.

To increase the recursion limit to 1500 we can add the following lines at the beginning of our program:

import sys
sys.setrecursionlimit(1500)

If you do that and try to calculate again the factorial of 1000 you get a long number back (no more errors).

The factorial of 1000 is: 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920
.......835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

That’s good! But…

…this solution could work if like in this case we are very near to the recursion limit and we are pretty confident that our program won’t end up using too much memory on our system.

How to Catch a Python Recursion Error

One possible option to handle the RecursionError exception is by using try except.

It allows to provide a clean message when your application is executed instead of showing an unclear and verbose exception.

Modify the “main” of your program as follows:

if __name__ == '__main__':
    try:
        print("The factorial of 1000 is: {}".format(factorial(1000)))
    except RecursionError as re:
        print("Unable to calculate factorial. Number is too big.") 

Note: before executing the program remember to comment the line we have added in the section before that increases the recursion limit for the Python interpreter.

Now, execute the code…

You will get the following when calculating the factorial for 1000.

$ python recursion_error.py
Unable to calculate factorial. Number is too big. 

Definitely a lot cleaner than the long exception traceback.

Interestingly, if we run our program with Python 2.7 the output is different:

$ python2 recursion_error.py 
Traceback (most recent call last):
  File "recursion_error.py", line 13, in <module>
    except RecursionError as re:
NameError: name 'RecursionError' is not defined 

We get back a NameError exception because the exception of type RecursionError is not defined.

Looking at the Python documentation I can see that the error is caused by the fact that the RecursionError exception was only introduced in Python 3.5:

RecursionError Python

So, if you are using a version of Python older than 3.5 replace the RecursionError with a RuntimeError.

if __name__ == '__main__':
    try:
        print("The factorial of 1000 is: {}".format(factorial(1000)))
    except RuntimeError as re:
        print("Unable to calculate factorial. Number is too big.") 

In this way our Python application works fine with Python2:

$ python2 recursion_error.py
Unable to calculate factorial. Number is too big. 

How Do You Stop Infinite Recursion in Python?

As we have seen so far, the use of recursion in Python can lead to a recursion error.

How can you prevent infinite recursion from happening? Is that even something we have to worry about in Python?

Firstly, do you think the code we have written to calculate the factorial could cause an infinite recursion?

Let’s look at the function again…

def factorial(n):
    if n == 0:
        return 1
    else:
        return n*factorial(n-1) 

This function cannot cause infinite recursion because the if branch doesn’t make a recursive call. This means that the execution of our function eventually stops.

We will create a very simple recursive function that doesn’t have an branch breaking the recursion…

def recursive_func():
    recursive_func()

recursive_func() 

When you run this program you get back “RecursionError: maximum recursion depth exceeded”.

$ python recursion_error2.py
Traceback (most recent call last):
  File "recursion_error2.py", line 4, in <module>
    recursive_func()
  File "recursion_error2.py", line 2, in recursive_func
    recursive_func()
  File "recursion_error2.py", line 2, in recursive_func
    recursive_func()
  File "recursion_error2.py", line 2, in recursive_func
    recursive_func()
  [Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded

So, in theory this program could have caused infinite recursion, in practice this didn’t happen because the recursion depth limit set by the Python interpreter prevents infinite recursion from occurring.

How to Convert a Python Recursion to an Iterative Approach

Using recursion is not the only option possible. An alternative to solve the RecursionError is to use a Python while loop.

We are basically going from recursion to iteration.

def factorial(n):
    factorial = 1

    while n > 0:
        factorial = factorial*n
        n = n - 1

    return factorial

Firstly we set the value of the factorial to 1 and then at each iteration of the while loop we:

  • Multiply the latest value of the factorial by n
  • Decrease n by 1

The execution of the while loop continues as long as n is greater than 0.

I want to make sure that this implementation of the factorial returns the same results as the implementation that uses recursion.

So, let’s define a Python list that contains a few numbers. Then we will calculate the factorial of each number using both functions and compare the results.

We use a Python for loop to go through each number in the list.

Our program ends as soon as the factorials calculated by the two functions for a given number don’t match.

def factorial(n):
    factorial = 1

    while n > 0:
        factorial = factorial*n
        n = n - 1

    return factorial

def recursive_factorial(n):
    if n == 0:
        return 1
    else:
        return n*factorial(n-1)

numbers = [4, 9, 18, 23, 34, 56, 78, 88, 91, 1000] 

for number in numbers:
    if factorial(number) != recursive_factorial(number):
        print("ERROR: The factorials calculated by the two functions for the number {} do not match.".format(number))

print("SUCCESS: The factorials calculated by the two functions match") 

Let’s run our program and see what we get:

$ python factorial.py
SUCCESS: The factorials calculated by the two functions match 

Great!

Our implementation of the factorial using an iterative approach works well.

Conclusion

In this tutorial we have seen why the RecursionError occurs in Python and how you can fix it.

Two options you have are:

  • Increase the value of the recursion limit for the Python interpreter.
  • Use iteration instead of recursion.

Which one are you going to use?

Claudio Sabato - Codefather - Software Engineer and Programming Coach

I’m a Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!

I have the following recursion code, at each node I call sql query to get the nodes belong to the parent node.

here is the error:

Exception RuntimeError: 'maximum recursion depth exceeded' in <bound method DictCursor.__del__ of <MySQLdb.cursors.DictCursor object at 0x879768c>> ignored

RuntimeError: maximum recursion depth exceeded while calling a Python object
Exception AttributeError: "'DictCursor' object has no attribute 'connection'" in <bound method DictCursor.__del__ of <MySQLdb.cursors.DictCursor object at 0x879776c>> ignored

Method that I call to get sql results:

def returnCategoryQuery(query, variables={}):
    cursor = db.cursor(cursors.DictCursor);
    catResults = [];
    try:
        cursor.execute(query, variables);
        for categoryRow in cursor.fetchall():
            catResults.append(categoryRow['cl_to']);
        return catResults;
    except Exception, e:
        traceback.print_exc();

I actually don’t have any issue with the above method but I put it anyways to give proper overview of the question.

Recursion Code:

def leaves(first, path=[]):
    if first:
        for elem in first:
            if elem.lower() != 'someString'.lower():
                if elem not in path:
                    queryVariable = {'title': elem}
                    for sublist in leaves(returnCategoryQuery(categoryQuery, variables=queryVariable)):
                        path.append(sublist)
                        yield sublist
                    yield elem

Calling the recursive function

for key, value in idTitleDictionary.iteritems():
    for startCategory in value[0]:
        print startCategory + " ==== Start Category";
        categoryResults = [];
        try:
            categoryRow = "";
            baseCategoryTree[startCategory] = [];
            #print categoryQuery % {'title': startCategory};
            cursor.execute(categoryQuery, {'title': startCategory});
            done = False;
            while not done:
                categoryRow = cursor.fetchone();
                if not categoryRow:
                    done = True;
                    continue;
                rowValue = categoryRow['cl_to'];
                categoryResults.append(rowValue);
        except Exception, e:
            traceback.print_exc();
        try:
            print "Printing depth " + str(depth);
            baseCategoryTree[startCategory].append(leaves(categoryResults))
        except Exception, e:
            traceback.print_exc();

Code to print the dictionary,

print "---Printing-------"
for key, value in baseCategoryTree.iteritems():
    print key,
    for elem in value[0]:
        print elem + ',';
    raw_input("Press Enter to continue...")
    print

If the recursion is too deep I should be getting the error when I call my recursion function, but when I get this error when I print the dictionary.

A Recursive function in programming is a function which calls itself. These functions find applications while constructing programs for factorial, Fibonacci series, Armstrong numbers, etc. The main idea is to break larger programs into smaller, less complex problems. With recursive functions, generating sequences becomes easy. But while using recursive functions, recursionerror may occur in python. In this article, we shall be looking into one such recursionerror: maximum recursion depth exceeded while calling a Python object

What is recursionerror?

As the name suggests, Recursionerror may occur when we are dealing with recursive functions. When we run the recursion function for a large number of times, recursion error is thrown. Python has a limit on the number of times a recursive function can call itself. This is done to ensure that the function does not execute infinitely and stops after some number of iterations. To know the recursion limit in python, we use the following code:

import sys
print(sys.getrecursionlimit())

The output is:

1000

Let us look at an example of RecursionError: maximum recursion depth exceeded. We shall take an example of a factorial function.

The following code shall generate factorial for a given number.

def find_fact(n):
  if n == 0 or n == 1:
    return 1
  else :
    return (n*find_fact(n-1))  

print("Factorial is :", find_fact(5))

Here, this program shall be executed successfully and shall print the below output:

Factorial is : 120

But if we pass a larger number into the find_fact() function, it will throw RecursionError: Maximum Recursion Depth Exceeded error.

print("Factorial is :", find_fact(5000))

Output:

RecursionError: maximum recursion depth exceeded in comparison

Since the recursion function exceeded the limit of 1000 iterations, recursionerror is thrown.

The RecursionError: Maximum Recursion Depth Exceeded error may also be thrown while we are trying to create a nested list whose length exceeds the recursion limit.

Let us take the following example. We have created a function named nested() which accepts one argument – n. Depending on the value of n, the length of that nested list would be created. Let us try to pass a value n greater than the recursion limit.

def nested(n): 
    list1 = list2 = [] 
    for i in range(n): 
        list1.append([])
        list1 = list1[0] 
    return list2

nestedlist = nested(2000)
print(nestedlist)

The output will be a recursion error.

RecursionError: maximum recursion depth exceeded while getting the repr of an object

RecursionError: Maximum Recursion Depth Exceeded While Calling A Python Object

The recursionerror for Maximum Recursion Depth Exceeded While Calling A Python Object is thrown when we are trying to call a python object in Django. The error may also occur while using Flask.

When the interpreter detects that the maximum depth for recursion has reached, it throws the recursionerror. To prevent the stack from getting overflow, python raises the recursionerror.

Best practices to avoid RecursionError: Maximum Recursion Depth Exceeded while calling a Python Object

1. Using other loops instead of recursion

To prevent the error from occurring, we can simply convert the piece of code from recursion to a loop statement.

If we take the example of the factorial function, we can convert it into a non – recursive function. We do that by placing a for loop inside the recursion function. The for loop will execute for a length equal to the value of the factorial number.

def find_fact(n):
  mul = 1
  for i in range(2,n+1):
    mul = mul * i
  return mul

print("Factorial is :", find_fact(1500))

Now, it will not throw any recursion error and simply print the large factorial number.

2. Using sys.setrecursionlimit() function

Else, if we still want to use the recursion function, we can increase the recursion limit from 1000 to a higher number. For that, we have to first import the sys library. Using the sys library, we will use the sys.setrecursionlimit() function.

import sys
sys.setrecursionlimit(2000)

Now, it will not thrown the recursionerror and the program will be executed for larger amount of recursions. On executing the recursive function, it will not throw any error and print its output.

def find_fact(n):
  if n == 0 or n == 1:
    return 1
  else :
    return (n*find_fact(n-1))  

print("Factorial is :", find_fact(1500))

3. Setting boundary conditions

It is necessary to set boundary conditions to ensures that the recursive function comes to an end. In the factorial program, the condition :

'if n == 1 or n == 0 : return 1'

is the boundary condition. It is with this condition that the loop comes to an end.

4. Creating a converging recursion

While writing the recursion condition, one has to ensure that the condition does come to an end and does not continue infinitely. The recursive calls should eventually tend towards the boundary condition.

We have to ensure that we creating a converging condition for that. In the factorial program, the ‘n*fact(n-1)’ is a converging condition that converges the value from n to 1.

5. Using Memoization

We can also use memoization to reduce the computing time of already calculated values. This way, we can speed up the calculations by remembering past calculations.

When recursive calls are made, then with memoization we can store the previously calculated values instead of unnecessarily calculating them again.


That sums up the article on RecursionError: Maximum Recursion Depth Exceeded While Calling A Python Object. If you have any questions in your mind, don’t forget to let us know in the comments below.

Until next time, Keep Learning!

  • [Solved] typeerror: unsupported format string passed to list.__format__

  • Solving ‘Remote End Closed Connection’ in Python!

  • [Fixed] io.unsupportedoperation: not Writable in Python

  • [Fixing] Invalid ISOformat Strings in Python!

In programming, recursive functions are routines or methods that call themselves directly or indirectly. Python limits the number of times a recursion function can call itself. If the recursion function exceeds that limit, Python raises the error

RecursionError: maximum recursion depth exceeded while calling a Python object

.

This tutorial will discuss the above error in detail and help you learn to debug and resolve it. We will also walk through an example to understand this error.

So let’s get started!

Recursion is a powerful technique to solve specific types of problems in programming. However, one must be very careful while dealing with recursive functions, as they may enter infinite loops.

Every recursive function has a base case that acts as a terminating condition. When a function meets the base case, it stops calling itself and returns the value back.

However, if the base case is not defined correctly or the recursive logic is incorrect, the function may end up calling itself infinitely. This means the function continues calling itself without any termination condition.

Calling any function occupies space in the memory. And calling a function inside a function for infinite times can occupy almost every part of your computer memory. To tackle this problem, Python has implemented a Recursion Depth limit.

According to the Python recursion depth limit, by default, a recursion function can all itself only 1000 times. If the recursion exceeds this limit, the interpreter throws the error


RecursionError: maximum recursion depth exceeded while calling a Python object




.

To know the default Recursion limit for your program, you can use the

getrecursionlimit()

method from the Python sys modules.


Example

import sys

print("This default recursion limit is :", sys.getrecursionlimit())


Output

This default recursion limit is : 1000

If we look at the recursion error statement, we can divide it into two parts

  1. RecursionError
  2. maximum recursion depth exceeded while calling a Python object


1. RecursionError

RecursionError is one of the Python standard exceptions. It is a module exception that comes under the Python RuntimeError. Python raises this exception when it detects a maximum recursion depth in a program.


2. maximum recursion depth exceeded while calling a Python object

The »

maximum recursion depth exceeded while calling a Python object

» statement is the error message that tags along with the RecursionError exception. This error message tells us that a Python function has exceeded the number of recursion calls.


Common Example Scenario

Let us write a Python program using recursion that prints the

nth

number from a Fibonacci series. You can even write this program using the for loop.

In the

Fibonacci series

, the first two numbers are 0 and 1; the following numbers are calculated with the sum of the previous two numbers.


Program

# recursive function to find the nth Fibonacci number
def n_fibonacci(n):
    if n==0:
        return 0
    elif n==1:
        return 1
    else:
        return n_fibonacci(n-1)+n_fibonacci(n-2)
#
n=10

print(f"The {n} st/nd/th Fibonacci number is: ",n_fibonacci(n-1))


Output

The 10 st/nd/th Fibonacci number is: 34

The above program is correct, and it also shows the correct output. But if we change the value of

n=10

to

n=1005

, it will raise the error.


Program

# recursive function to find the nth Fibonacci number
def n_fibonacci(n):
    if n==0:
        return 0
    elif n==1:
        return 1
    else:
        return n_fibonacci(n-1)+n_fibonacci(n-2)
#out of the recursion range
n=1005

print(f"The {n}st/nd/th Fibonacci number is: ",n_fibonacci(n-1))


Output

RecursionError: maximum recursion depth exceeded in comparison

You can see that we receive the RecursionError with a different Error message. This is because the error message changes according to the operation we perform inside the function.

Here, it displays

»

maximum recursion depth exceeded in comparison

«.

This is because after exceeding the recursion limit, the Python interpreter can also not perform the comparison operator inside the recursion.


Solution

Python provides a

setrecursionlimit()

method that accepts an integer value as an argument and sets it as a recursion limit for the program. We can use this method to increase the default recursion depth limit.


Note:

The

setrecursionlimit()

method is also limited and can only increase the recursion limit depth to 3500.

To solve the above example, we can increase the recursion limit to 2000 using the

setrecursionlimit()

method.


Example

import sys
# increase the recursion limit
sys.setrecursionlimit(2000)

# recursive function to find the nth fibonacci number
def n_fibonacci(n):
    if n==0:
        return 0
    elif n==1:
        return 1
    else:
        return n_fibonacci(n-1)+n_fibonacci(n-2)
#now in recursion range
n=1005

print(f"the {n}st/nd/th fibonacci number is: ",n_fibonacci(n-1))


Output

the 1005 st/nd/th fibonacci number is: 482051511617926448416241857411039626258600330733909004920469712704382351844831823569922886993050824175326520025449797859766560885196970738202943545195859929088936259370887605815413541849563887924611727164704130

Executing the above program may take 10 to 20 minutes to finish because it calls the function repeatedly 2000 times.


Wrapping Up!

The

RecursionError

occurs when a recursion call exceeds the default recursion depth limit. When you encounter this error in your Python program, you must consider using an iterative approach to solve the problem. Using iterative statements like

for

and

while

loop, we can perform the desired action quickly and efficiently.

However, if you wish to solve your problem recursively, in that case, you can use them

setrecursivelimit()

to increase the default limit of the recursion call.

If you still get this error in your Python program, you can share your code in the comment section. We will try to help you with debugging.


People are also reading:

  • Python FileNotFoundError: [Errno 2] No such file or directory Solution

  • How to Remove Last Character from Python String?

  • Python SyntaxError: non-default argument follows default argument Solution

  • What is CubicWeb in Python?

  • Python TypeError: cannot unpack non-iterable NoneType object Solution

  • What is Python Pickle Module?

  • Python IndentationError: expected an indented block Solution

  • What is Web2Py in Python?

  • Python NameError: name ‘self’ is not defined Solution

  • What is TurboGears in Python?

Понравилась статья? Поделить с друзьями:
  • Recpage dll ошибка
  • Recovery ошибка no command
  • Recovery ошибка 0xc000000d
  • Rdp произошла внутренняя ошибка windows server 2012 r2
  • Recovery как убрать ошибку