2012-04-25 7 views
0

に「和()」を使用したときに「NoneType」オブジェクトは、私は、リストの合計を見つけようとするとき、私ははTypeError:リスト

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 

おかげで問題が発生した

答えて

5

これは間違っている:.appendから

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

戻り値は、したがって、エラーNoneです。

+2

また、あなたはその '候補= []'デフォルト引数を指定して、大きなトラブルに自分自身を取得するつもりです。これは関数定義時に評価されるため、クラスのすべてのインスタンスは同じリストを共有します(別のリストが渡されない限り)。リストのコピーを助言してください: 'self._candidates = candidates [:]' – kindall

関連する問題