Python: Maximum recursion depth exceeded

可紊 提交于 2019-11-26 02:09:06

问题


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.


回答1:


You can increment the stack depth allowed - with this, deeper recursive calls will be possible, like this:

import sys
sys.setrecursionlimit(10000) # 10000 is an example, try with different values

... But I'd advise you to first try to optimize your code, for instance, using iteration instead of recursion.



来源:https://stackoverflow.com/questions/8177073/python-maximum-recursion-depth-exceeded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!