a = list().append(“hello”)vs a = list();在python中a.append(“你好”)?

a = list().append(“hello”)vs a = list();在python中a.append(“你好”)?,第1张

概述我有 try: a = list().append('hello') 但是a是NoneType try: b = list() b.append('hello') 和b是列表类型 我认为list()返回一个列表对象,而list().append(‘hello’)将使用返回列表来追加,但为什么是None的值? list()确实返回一个空列表([]),但append方法在就地列 我有

@H_301_11@try: a = List().append('hello')

但是a是nonetype

@H_301_11@try: b = List() b.append('hello')

和b是列表类型

我认为List()返回一个列表对象,而List().append(‘hello’)将使用返回列表来追加,但为什么是None的值?

解决方法 List()确实返回一个空列表([]),但append方法在就地列表上运行 – 它更改列表本身,并且不返回新列表.它返回None.

例如:

@H_301_11@>>> lst = []>>> lst.append('hello') # appends 'hello' to the List>>> lst['hello']>>> result = lst.append('world') # append method returns None>>> result # nothing is displayed>>> print resultNone>>> lst # the List contains 'world' as well Now['hello','world'] 总结

以上是内存溢出为你收集整理的a = list().append(“hello”)vs a = list();在python中a.append(“你好”)?全部内容,希望文章能够帮你解决a = list().append(“hello”)vs a = list();在python中a.append(“你好”)?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1194890.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存