为什么[] .append(1)是None

tdo*_*ong 4 python

我像这样使用Python

>>>print [].append(1)
None

>>>ls = []
>>>ls.append(1)
>>>ls
[1]
Run Code Online (Sandbox Code Playgroud)

为什么"[] .append(1)"值是无,而另一个是真正的价值?

unw*_*ind 13

因为append()list方法不返回列表,所以它只修改调用它的列表.在这种情况下,将修改匿名列表,然后将其丢弃.

文档不是很清楚,但它说的只是:

list.append(x)的

将项添加到列表的末尾; 相当于a[len(a):] = [x].

对于其他方法,例如list.count(x),"return"一词出现在描述中,暗示如果没有,则该方法没有返回值.