2013-06-01 13 views
5

私は文字列を返すクラスインスタンスを使用しています。IDLEが文字列であるといっても、クラスインスタンスから返された文字列がNoneType型を保持するのはなぜですか?

私はこのインスタンスを2回呼び出し、返された値をリストに集めています。次に、.sort()を使用してこれら2つの文字列をソートしようとしています。しかし、私がそうすると、型が(Nonetype - それをオブジェクトとみなす)というエラーがスローされます。

タイプ(リストの要素)でチェックして、タイプ "文字列"を返しました。私は何が起こっているのか分からない。基本的にアイドル状態では、リストに文字列があると言われますが、実行時にNoneType(これらの文字列のリスト)が反復可能ではないというエラーがスローされます。ここ

は一例です:

list_of_strings = [class_method(args), class_method(another_args)] ## this instance returns string 

print type(list_of_strings[0]) #prints type 'str' 

はERROR:

list_sorted = list(list_of_strings.sort()) 
TypeError: 'NoneType' object is not iterable 

どうもありがとう!

7. The sort() and reverse() methods modify the list in place for economy of space when sorting or reversing a large list. To remind you that they operate by side effect, they don’t return the sorted or reversed list.

使用sorted():pythonのdocumentationから

ジョージ

答えて

3

list_sorted = list(sorted(list_of_strings)) 
+0

ありがとうElaszar!出来た。ジョージ – GeorgeG

0

あなたはsorted()を使用する必要があります。

list_sorted = list(sorted(list_of_strings)) 

.sort()呼び出しの結果であるNonelist()を呼び出しています。

関連する問題