Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why string returned from a class instance keeps NoneType type even though IDLE says it is a string?

I am using a class instance which returns a string.

I am calling twice this instance and collecting returned values into a list. Then am trying to use .sort() to sort these two strings. However, when I do so it throws an error saying that the type is (Nonetype - considers it as object).

I did check with type(element of a list) and it returned type "string". I have no clue what's going on. Basically in idle it says that I have strings in a list.. but when running it throws an error saying that the NoneType (list of these strings) is not iterable.

here is an example:

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

Thanks a lot!

George

like image 874
GeorgeG Avatar asked Oct 21 '22 08:10

GeorgeG


1 Answers

From the python 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.

Use sorted():

list_sorted = list(sorted(list_of_strings))
like image 71
Elazar Avatar answered Oct 28 '22 21:10

Elazar