gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:14:02 25 4
gpt4 key购买 nike

我注意到,对列表进行的许多修改列表内容的操作都会返回 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'treturn 'self'.

This comes from a coding style (popular in various other languages, Ibelieve especially Lisp revels in it) where a series of side effectson 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 thereader must be intimately familiar with each of the methods. Thesecond form makes it clear that each of these calls acts on the sameobject, and so even if you don't know the class and its methods verywell, you can understand that the second and third call are applied tox (and that all calls are made for their side-effects), and not tosomething 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 ofside-effect calls (pstat comes to mind). There shouldn't be any newones; pstat slipped through my filter when it was weak.

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com