Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当使用extend函数扩展列表而不将其分配给内存时,返回None_Python_Python 2.7 - Fatal编程技术网

Python 当使用extend函数扩展列表而不将其分配给内存时,返回None

Python 当使用extend函数扩展列表而不将其分配给内存时,返回None,python,python-2.7,Python,Python 2.7,尝试执行此操作时,它将返回None x = [1,2,3].extend([4,5,6]) 但是[1,2,3]+[4,5,6]这很好用,有人能告诉我为什么吗 我的意思是extend()函数采用相同的格式,为什么它不返回任何值?帮助(list.extend)将提供如下内容: extend(...) L.extend(iterable) -> None -- extend list by appending elements from the iterable 因此,extend确

尝试执行此操作时,它将返回
None

x = [1,2,3].extend([4,5,6])
但是
[1,2,3]+[4,5,6]
这很好用,有人能告诉我为什么吗

我的意思是
extend()
函数采用相同的格式,为什么它不返回任何值?

帮助(list.extend)
将提供如下内容:

extend(...)
    L.extend(iterable) -> None -- extend list by appending elements from the iterable
因此,
extend
确实合并两个列表,但返回
None
,因为这是一个就地操作。例如:

>>> a = [1,2,3]
>>> print(a.extend([4,5,6]))
None
>>> a
[1, 2, 3, 4, 5, 6]

extend
更改调用它的列表,并返回
None
。如果你想要像
[1,2,3]+[4,5,6]
这样的东西,那么就使用它。
x=[1,2,3]
然后
x.extend(4,5,6)
会给你结果。
list.extend()
会将其他iTrable转换为,而串联则不会
list.extend()。