加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 为什么从类实例返回的字符串保持NoneType类型,即使IDL

发布时间:2020-12-16 21:37:46 所属栏目:Python 来源:网络整理
导读:我正在使用一个返回字符串的类实例. 我正在调用此实例两次并将返回的值收集到列表中.然后我尝试使用.sort()来对这两个字符串进行排序.但是,当我这样做时,它会抛出一个错误,说明类型是(Nonetype – 将其视为对象). 我确实检查了类型(列表的元素),并返回了类型
我正在使用一个返回字符串的类实例.

我正在调用此实例两次并将返回的值收集到列表中.然后我尝试使用.sort()来对这两个字符串进行排序.但是,当我这样做时,它会抛出一个错误,说明类型是(Nonetype – 将其视为对象).

我确实检查了类型(列表的元素),并返回了类型“string”.我不知道发生了什么.基本上在空闲时它表示我在列表中有字符串..但是在运行它时会抛出一个错误,说NoneType(这些字符串的列表)不可迭代.

这是一个例子:

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

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

错误:

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

非常感谢!

乔治

解决方法

从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.

使用sorted():

list_sorted = list(sorted(list_of_strings))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读