为什么 append() 在 Python 中总是返回 None ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/16641119/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does append() always return None in Python?
提问by Nima Vaziri
list = [1, 2, 3]
print list.append(4)   ## WRONG, print does not work, append() returns None
## RIGHT:
list.append(4)
print list  ## [1, 2, 3, 4]
I'm learning Python and I'm not sure if this problem is specific to the language and how appendis implemented in Python.
我正在学习 Python,我不确定这个问题是否特定于语言以及如何append在 Python 中实现。
采纳答案by xuanji
appendis a mutating (destructive) operation (it modifies the list in place instead of of returning a new list). The idiomatic way to do the non-destructive equivalent of appendwould be
append是一个变异(破坏性)操作(它修改列表而不是返回一个新列表)。做无损等效的惯用方法append是
l = [1,2,3]
print l + [4] # [1,2,3,4]
print l # [1,2,3]
to answer your question, my guess is that if appendreturned the newly modified list, users might think that it was non-destructive, ie they might write code like
回答你的问题,我的猜测是,如果append返回新修改的列表,用户可能会认为它是非破坏性的,即他们可能会编写这样的代码
m = l.append("a")
n = l.append("b")
and expect nto be [1,2,3,"b"]
并期望n成为[1,2,3,"b"]
回答by jbiz
One word of advice would be to avoid using key words or functions as variable names. In your code above, you use list as a variable:
建议之一是避免使用关键字或函数作为变量名。在上面的代码中,您使用 list 作为变量:
list = [1, 2, 3]
I would advise against using listas a variable name as listis actually already defined as a builtin type.  As ChaseTheSun and squiguy pointed out, there isn't much more to it then
我建议不要list用作变量名,因为列表实际上已经定义为内置类型。正如 ChaseTheSun 和 squiguy 指出的那样,没有更多了
l = [1, 2, 3]
l.append(4)
print l  ## [1, 2, 3, 4]
回答by Steven Rumbalski
It is a convention in Python that methods that mutate sequences return None.
Python 中的一个约定是,变异序列的方法返回None.
Consider:
考虑:
>>> a_list = [3, 2, 1]
>>> print a_list.sort()
None
>>> a_list
[1, 2, 3]
>>> a_dict = {}
>>> print a_dict.__setitem__('a', 1)
None
>>> a_dict
{'a': 1}
>>> a_set = set()
>>> print a_set.add(1)
None
>>> a_set
set([1])
Starting in Python 3.3, this is now more explicitly documented:
从 Python 3.3 开始,现在更明确地记录了这一点:
Some collection classes are mutable. The methods that add, subtract, or rearrange their members in place, and don't return a specific item, never return the collection instance itself but
None.
一些集合类是可变的。添加、减去或重新排列其成员的方法,并且不返回特定项,从不返回集合实例本身,而是
None.
The Design and History FAQ gives the reasoningbehind this design decision (with respect to lists):
设计和历史常见问题解答给出了这个设计决定背后的推理(关于列表):
Why doesn't
list.sort() return the sorted list?In situations where performance matters, making a copy of the list just to sort it would be wasteful. Therefore,
list.sort()sorts the list in place. In order to remind you of that fact, it does not return the sorted list. This way, you won't be fooled into accidentally overwriting a list when you need a sorted copy but also need to keep the unsorted version around.In Python 2.4 a new built-in function –
sorted()– has been added. This function creates a new list from a provided iterable, sorts it and returns it.
为什么不
list.sort()返回排序列表?在性能很重要的情况下,复制列表只是为了对其进行排序将是一种浪费。因此,
list.sort()将列表排序到位。为了提醒您这一事实,它不会返回排序列表。这样,当您需要一个排序的副本但还需要保留未排序的版本时,您就不会被愚弄而意外覆盖列表。在 Python 2.4 中添加了一个新的内置函数 –
sorted()–。此函数从提供的可迭代对象创建一个新列表,对其进行排序并返回它。
回答by Rajendra s
It does not return anything. it appends/add to the variable, to see you should use the first variable which used to append in print
它不返回任何东西。它附加/添加到变量,以查看您应该使用用于附加打印的第一个变量
friends=["Rajendra V"]
friends.append("John")
print(friends)

