简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM