2013-06-01 10 views
5

Bir dize döndüren bir sınıf örneği kullanıyorum.Bir sınıf örneğinden döndürülen dize, neden bir dizge olduğunu söylese de NoneType türü tutar?

Bu örnekte iki kez ve döndürülen değerleri bir listeye topluyorum. Sonra bu iki dizeleri sıralamak için .sort() kullanmaya çalışıyorum. Ancak, bunu yaptığımda, türün (Nonetype - bunu nesne olarak kabul eder) olduğunu söyleyen bir hata atar.

Türüyle (bir listenin öğesi) kontrol ettim ve "string" türünü döndürdü. Neler olduğu hakkında hiçbir fikrim yok. Temel olarak boşta bir listede dizeleri olduğunu söylüyor .. ama çalıştırırken NoneType (bu dizelerin listesi) yinelenen olmadığını söyleyerek bir hata atar.

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

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

HATA:

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

Çok teşekkürler burada

bir örnektir!

George

piton documentation itibaren

cevap

3

:

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.

Kullanım sorted():

list_sorted = list(sorted(list_of_strings)) 
+0

Teşekkür Elaszar! İşe yaradı. George – GeorgeG

0

Sen kullanmalıdır sorted():

list_sorted = list(sorted(list_of_strings)) 

Şu anda .sort() aramasının sonucu olan None numaralı telefondan list() numaralı telefonu arıyorsun.

İlgili konular