类型错误:“NoneType”对象不是可迭代使用时“和()”列表

问题描述:

上给予我收到类型错误:“NoneType”对象不是可迭代使用时“和()”列表

TypeError: 'NoneType' object is not iterable 

,当我试图找到一个列表的总和。

发生问题:

if(sum(self._candidates) + self._allCandidates[self._depth]._weight > 20): 
    self._left = Node(self._candidates, self._depth + 1, self._allCandidates) 
else: 
    self._left = Node(self._candidates.append(self._allCandidates[self._depth]), self._depth + 1, self._allCandidates) 

节点定义:

def __init__(self, candidates = [], depth = -1, allCandidates = []): 
     self._candidates = candidates 
     self._depth = depth 
     self._allCandidates = allCandidates 

感谢在这个问题上的任何帮助。

这是错误的:

Node(self._candidates.append(self._allCandidates[self._depth]) 

返回值从.appendNone,因此错误。

+2

另外,你会因为'candidate = []'默认参数而让自己陷入严重的麻烦。这是在函数定义时评估的,因此该类的所有实例将共享相同的列表(除非传入另一个列表)。建议复制清单:'self._candidates = candidates [:]' – kindall 2012-04-25 16:56:45