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

StackOverflow https://stackoverflow.com/questions/16877338

Question

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

Was it helpful?

Solution

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))

OTHER TIPS

You should use sorted():

list_sorted = list(sorted(list_of_strings))

You are now calling list() on None which is the result of the .sort() call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top