TypeError:'NoneType'对象不可迭代

时间:2012-08-30 14:52:14

标签: python recursion typeerror nonetype

我有一个函数“checkAllInOneDirection”,它是一个递归循环。当我突破循环时,该函数返回1个数组和3个布尔值。最奇怪的是,在递归函数中,当它返回时,它显示所有值都有值但我仍然得到“无类型”错误:

这是印刷品:

Before return : [[0, 0]] False False False False

但是当它返回并且异常输出之前,返回的值变为

    After returned : [[0, 0]] True True True True

虽然递归后的返回值发生了变化并且出错了,但仍然有一些值,但是我出现了“无类型”错误的异常?这是错误:

     File "C:\xampp\htdocs\ZoneFinding\zoneFinder2D_V2.py", line 127, in handleCheck
    finalCatch, forBoo, bakBoo, upBoo, dwnBoo = checkAllInOneDirection(finalCatch,tempCatch,recursiveCount,newCatch, columnCount, rowCount, width, height, posToCheck, forBoo, bakBoo, upBoo, dwnBoo)
TypeError: 'NoneType' object is not iterable

这是调用递归“checkAllInOneDirection”的函数。

def handleCheck(newCatch, locale, subset, width, height, rowCount, columnCount, posToCheck):
    forBoo = True; bakBoo =True; upBoo=True; dwnBoo= True; lastOneInSet =0;
    while forBoo and bakBoo and upBoo and dwnBoo :
        if locale[posToCheck[0]][posToCheck[1]] == 0:
            recursiveCount = 0; tempCatch = []; finalCatch =[]
            tempCatch.append(posToCheck)
            finalCatch, forBoo, bakBoo, upBoo, dwnBoo = checkAllInOneDirection(finalCatch,tempCatch,recursiveCount,newCatch, columnCount, rowCount, width, height, posToCheck, forBoo, bakBoo, upBoo, dwnBoo)
            replaceVal(locale, finalCatch)

    return False, finalCatch

这是递归函数checkAllInOneDirection代码。

def checkAllInOneDirection(finalCatch,tempCatch,recursiveCount,newCatch, width, height, forBoo, bakBoo, upBoo, dwnBoo):

    isItLast = checkLast(forBoo, bakBoo, upBoo, dwnBoo)
    if isItLast:
        for each in tempCatch:
            if not each in finalCatch:
                finalCatch.append(each)
        tempCatch=[]
        for each in newCatch:
            if not each in finalCatch:
                finalCatch.append(each)
        newCatch=[]
        print "Before return :", finalCatch, forBoo, bakBoo, upBoo, dwnBoo
        return finalCatch, forBoo, bakBoo, upBoo, dwnBoo

    for each in range (0, len(tempCatch)):
        posToCheck = posToCheckBak = posToCheckUp = posToCheckDwn = [tempCatch[each][0], tempCatch[each][1]]
        ### Some code to check position forward
        newPosForward = checkForward(posToCheck, width)
        if newPosForward != False:
            tempLocale = locale[newPosForward[0]][newPosForward[1]]
        elif newPosForward == False:
            tempLocale = 1
        if newPosForward != False and tempLocale ==0 and not newPosForward in finalCatch and not newPosForward in newCatch:
            forVal = locale[newPosForward[0]][newPosForward[1]]
            newCatch.append(newPosForward)
            posToCheck = newPosForward
            forBoo = True
        elif newPosForward == False and tempLocale == 1 and not newPosForward in newCatch:
            forBoo = False

        ### Some code to check position backward
        newPosBackward = checkBackward(posToCheckBak)
        if newPosBackward != False:
            tempLocale = locale[newPosBackward[0]][newPosBackward[1]]
        elif newPosBackward == False:
            tempLocale = 1    
        if newPosBackward != False and tempLocale ==0 and not newPosBackward in finalCatch and not newPosBackward in newCatch:
            forVal = locale[newPosBackward[0]][newPosBackward[1]]
            newCatch.append(newPosBackward)
            posToCheckBak = newPosBackward
            bakBoo = True
        elif newPosBackward == False and tempLocale == 1 and not newPosBackward in newCatch:
            bakBoo = False

        ### Some code to check position upward
        newPosUp = checkUpRow(posToCheckUp)
        if newPosUp != False:
            tempLocale = locale[newPosUp[0]][newPosUp[1]]
        elif newPosUp == False:
            tempLocale = 1
        if newPosUp != False and tempLocale ==0 and not newPosUp in finalCatch and not newPosUp in newCatch:
            forVal = locale[newPosUp[0]][newPosUp[1]]
            newCatch.append(newPosUp)
            posToCheckUp = newPosUp
            upBoo = True
        elif newPosUp == False and tempLocale == 1 and not newPosUp in newCatch:
            upBoo = False

        ### Some code to check position downward
        newPosDwn = checkDwnRow(posToCheckDwn, height)
        if newPosDwn != False:
            tempLocale = locale[newPosDwn[0]][newPosDwn[1]]
        elif newPosDwn == False:
            tempLocale = 1
        if newPosDwn != False and tempLocale ==0 and not newPosDwn in finalCatch and not newPosDwn in newCatch:
            forVal = locale[newPosDwn[0]][newPosDwn[1]]
            newCatch.append(newPosDwn)
            posToCheckDwn = newPosDwn
            dwnBoo = True
        elif newPosDwn == False and tempLocale == 1 and not newPosDwn in newCatch:
            dwnBoo = False

    for each in tempCatch:
        if not each in finalCatch:
            finalCatch.append(each)
    tempCatch =[]
    for each in newCatch:    
        if not each in finalCatch:
            finalCatch.append(each)
            tempCatch.append(each)
    newCatch = []

    checkAllInOneDirection(finalCatch,tempCatch,recursiveCount,newCatch, width, height, forBoo, bakBoo, upBoo, dwnBoo)

2 个答案:

答案 0 :(得分:7)

我想你想要最后一行:

return checkAllInOneDirection(...)

而不只是:

checkAllInOneDirection(...)

在该语句结束后,该函数返回。由于它没有明确的return语句,因此返回None

答案 1 :(得分:1)

  

finalCatch,forBoo,bakBoo,upBoo,dwnBoo = checkAllInOneDirection(...)

期望在等于右侧的a,b,c,d,e形式。这是listtuplerange的语法快捷方式。如果一个函数没有返回,或者返回一些无法迭代的函数,那就是你期望的那种错误。

迭代意味着以下内容:假设您有一个类作为值的容器。对类或值进行迭代是逐个检查每个值。如果你正在检查一个不支持这种行为的类(例如整数),它会抛出一个错误,因为无法处理请求。