2013-06-01 6 views
5

Sto usando un'istanza di classe che restituisce una stringa.Perché la stringa restituita da un'istanza di classe mantiene NoneType anche se IDLE dice che è una stringa?

Sto chiamando due volte questa istanza e raccogli i valori restituiti in un elenco. Quindi sto cercando di usare .sort() per ordinare queste due stringhe. Tuttavia, quando lo faccio, genera un errore che dice che il tipo è (Nonetype - lo considera come oggetto).

Ho controllato con tipo (elemento di una lista) e ha restituito il tipo "stringa". Non ho idea di cosa sta succedendo. Fondamentalmente inattivo dice che ho delle stringhe in una lista .. ma quando lo si esegue genera un errore che dice che NoneType (lista di queste stringhe) non è iterabile.

Ecco un esempio:

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

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

ERRORE:

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

Grazie mille!

George

risposta

3

Dal pitone documentation:

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.

Uso sorted():

list_sorted = list(sorted(list_of_strings)) 
+0

Grazie Elaszar! Ha funzionato. Giorgio – GeorgeG

0

si dovrebbe usare sorted():

list_sorted = list(sorted(list_of_strings)) 

Ora chiami list() su None che è il risultato della chiamata .sort().