Python extend with an empty list bug? [duplicate]

孤者浪人 提交于 2019-11-27 04:58:47

Extend is a method of list, which modifies it but doesn't return self (returning None instead). If you need the modified value as the expression value, use +, as in [2]+[].

Exactly.

>>> x = [2]
>>> x.extend([]) # Nothing is printed because the return value is None
>>> x == [2]
True
>>> x
[2]

They do this on purpose so that you will remember that the extend function is actually modifying the list in-place. Same with sort(). It always returns None.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!