Python 类型错误:';非类型';对象在使用';sum()';名单上
我越来越Python 类型错误:';非类型';对象在使用';sum()';名单上,python,list,sum,nonetype,Python,List,Sum,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
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])
.append
的返回值为None
,因此出现错误。这是错误的:
Node(self._candidates.append(self._allCandidates[self._depth])
.append
的返回值是None
,因此出现了错误。此外,使用candidates=[]
默认参数会给您带来很大的麻烦。这是在函数定义时计算的,因此该类的所有实例将共享同一个列表(除非传入另一个列表)。建议复制列表:self.\u candidates=candidates[:]
另外,您将在使用candidates=[]
默认参数时遇到大麻烦。这是在函数定义时计算的,因此该类的所有实例将共享同一个列表(除非传入另一个列表)。建议复制列表:self.\u候选者=候选者[:]