python - 为什么这些列表方法(追加、排序、扩展、删除、清除、反转)返回 None 而不是结果列表?

标签 python list

我注意到,对列表进行的许多修改列表内容的操作都会返回 None ,而不是返回列表本身。示例:

>>> mylist = ['a', 'b', 'c']
>>> empty = mylist.clear()
>>> restored = mylist.extend(range(3))
>>> backwards = mylist.reverse()
>>> with_four = mylist.append(4)
>>> in_order = mylist.sort()
>>> without_one = mylist.remove(1)
>>> mylist
[0, 2, 4]
>>> [empty, restored, backwards, with_four, in_order, without_one]
[None, None, None, None, None, None]

这个决定背后的思考过程是什么?

对我来说,这似乎是一种阻碍,因为它阻止了列表处理的“链接”(例如 mylist.reverse().append('a string')[:someLimit] )。我想“The Powers That Be”可能认为列表理解是一个更好的范例(一个有效的意见),因此不想鼓励其他方法 - 但阻止直觉方法似乎是有悖常理的,即使更好存在替代方案。

<小时/>

这个问题具体是关于 Python 的设计决策返回 None来自改变列表方法,如 .append 。然而,新手经常会编写错误的代码,期望 .append (特别是)返回刚刚修改的相同列表。但是,将此类问题作为与此问题重复的问题来关闭。 “代码做了错误的事情,因为结果是 None 而不是列表”是这些情况下的 OP 应该通过调试独立发现的;创建正确的 MRE 会留下这样的问题 - 因此,它可以被认为是重复的。

参见How can I collect the results of a repeated calculation in a list, dictionary etc. (make a copy of a list with each element modified)?对于“如何重复附加到列表?”这样的简单问题(或调试归结为该问题的问题)。这是一个新的规范,专门以初学者缺乏的视角来解决该主题。

要获取列表的修改版本,请参阅:

<子>

同样的问题也适用于其他内置数据类型的某些方法,例如set.discard (参见How to remove specific element from sets inside a list using list comprehension)和dict.update (参见Why doesn't a python dict.update() return the object?)。

同样的推理也适用于设计您自己的 API。请参阅Is making in-place operations return the object a bad idea? .

最佳答案

Python 中的一般设计原则是就地改变对象以返回None的函数。我不确定这是否是我选择的设计选择,但它基本上是为了强调不会返回新对象。

Guido van Rossum(我们的 Python BDFL)说明了设计选择 on the Python-Dev mailing list :

I'd like to explain once more why I'm so adamant that sort() shouldn't return 'self'.

This comes from a coding style (popular in various other languages, I believe especially Lisp revels in it) where a series of side effects on a single object can be chained like this:

x.compress().chop(y).sort(z)

which would be the same as

x.compress()
x.chop(y)
x.sort(z)

I find the chaining form a threat to readability; it requires that the reader must be intimately familiar with each of the methods. The second form makes it clear that each of these calls acts on the same object, and so even if you don't know the class and its methods very well, you can understand that the second and third call are applied to x (and that all calls are made for their side-effects), and not to something else.

I'd like to reserve chaining for operations that return new values, like string processing operations:

y = x.rstrip("\n").split(":").lower()

There are a few standard library modules that encourage chaining of side-effect calls (pstat comes to mind). There shouldn't be any new ones; pstat slipped through my filter when it was weak.

关于python - 为什么这些列表方法(追加、排序、扩展、删除、清除、反转)返回 None 而不是结果列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54848884/

相关文章:

python - 满足特定条件的 Pandas Dataframe 正向填充

list - number_in_month 练习(SML 列表迭代)

python - Matplotlib:如何从数组的列中绘制多条线,但给它们一个标签?

python - 斐波那契数列 Mod 1000000007

c# - 多线程网络抓取工具的最佳解决方案?

list - 列表理解 Haskell 中的意外并行语句

python - 将 numpy 数组列表转换为 torch 张量列表

c# 列表查找重复更新多个列表

python - 向 azure 发送 iot 设备消息

python - 如何使用 logging.config.fileConfig() 自定义时间格式