简体   繁体   English

为什么 Python 在 list.reverse() 上返回 None ?

[英]Why does Python return None on list.reverse()?

Was solving an algorithms problem and had to reverse a list.正在解决算法问题并且不得不反转列表。 When done, this is what my code looked like:完成后,这就是我的代码的样子:

def construct_path_using_dict(previous_nodes, end_node):
    constructed_path = []
    current_node = end_node
    while current_node:
        constructed_path.append(current_node)
        current_node = previous_nodes[current_node]
    constructed_path = reverse(constructed_path)    
    return constructed_path

But, along the way, I tried return constructed_path.reverse() and I realized it wasn't returning a list... Why was it made this way?但是,在此过程中,我尝试return constructed_path.reverse() _路径.reverse return constructed_path.reverse()并且我意识到它没有返回一个列表......为什么会这样?
Shouldn't it make sense that I should be able to return a reversed list directly, without first doing list.reverse() or list = reverse(list) ?我应该能够直接返回一个反向列表,而不首先执行list.reverse()list = reverse(list)是否list.reverse()

list.reverse reverses in place, modifying the list it was called on. list.reverse反转,修改它被调用的列表。 Generally, Python methods that operate in place don't return what they operated on to avoid confusion over whether the returned value is a copy.通常,就地操作的 Python 方法不会返回它们操作的内容,以避免混淆返回值是否为副本。

You can reverse and return the original list:您可以反转并返回原始列表:

constructed_path.reverse()
return constructed_path

Or return a reverse iterator over the original list, which isn't a list but doesn't involve creating a second list just as big as the first:或者在原始列表上返回一个反向迭代器,它不是一个列表,但不涉及创建与第一个列表一样大的第二个列表:

return reversed(constructed_path)

Or return a new list containing the reversed elements of the original list:或者返回一个包含原始列表反转元素的新列表:

return constructed_path[::-1]
# equivalent: return list(reversed(constructed_path))

If you're not concerned about performance, just pick the option you find most readable.如果您不关心性能,只需选择您认为最易读的选项。

What I'm about to write was already said here, but I'll write it anyway because I think it will perhaps add some clarity.我要写的东西已经在这里说了,但我还是会写它,因为我认为它可能会增加一些清晰度。

You're asking why the reverse method doesn't return a (reference to the) result, and instead modifies the list in-place.您在问为什么reverse方法不返回(引用)结果,而是就地修改列表。 In the official python tutorial , it says this on the matter:官方 python 教程中,它对此事说:

You might have noticed that methods like insert, remove or sort that only modify the list have no return value printed – they return the default None.你可能已经注意到像 insert、remove 或 sort 这样只修改列表的方法没有打印返回值——它们返回默认值 None。 This is a design principle for all mutable data structures in Python.这是 Python 中所有可变数据结构的设计原则。

In other words (or at least, this is the way I think about it) - python tries to mutate in-place where-ever possible (that is, when dealing with an immutable data structure), and when it mutates in-place, it doesn't also return a reference to the list - because then it would appear that it is returning a new list, when it is really returning the old list.换句话说(或者至少,这是我的想法)——python 尝试在任何可能的地方就地变异(即,在处理不可变数据结构时),并且当它就地变异时,它不返回列表的引用-因为那时它会出现它返回一个新的列表,当它真的返回旧列表。

To be clear, this is only true for object methods , not functions that take a list, for example, because the function has no way of knowing whether or not it can mutate the iterable that was passed in. Are you passing a list or a tuple ?需要明确的是,这仅适用于对象方法,而不适用于采用列表的函数,例如,因为该函数无法知道它是否可以改变传入的可迭代对象。您传递的是list还是list tuple The function has no way of knowing, unlike an object method.与对象方法不同,该函数无法知道。

methods like insert, remove or sort that only modify the list have no return value printed – they return the default None.像 insert、remove 或 sort 这样只修改列表的方法没有打印返回值——它们返回默认的 None。 1 This is a design principle for all mutable data structures in Python. 1 这是 Python 中所有可变数据结构的设计原则。

PyDocs 5.1文档 5.1

As I understand it, you can see the distinction quickly by comparing the differences returned by modifying a list (mutable) ie using list.reverse() and mutating a list that's an element within a tuple (non-mutable), while calling据我了解,您可以通过比较通过修改列表(可变)返回的差异快速看到区别,即使用 list.reverse() 并改变作为元组中元素的列表(非可变),同时调用

id(list)

id(tuple_with_list)

before and after the mutations.突变前后。 Mutable data-type mutations returning none is part allowing them to be changed/expanded/pointed-to-by-multiple references without reallocating memory.返回 none 的可变数据类型突变​​是允许它们在不重新分配内存的情况下被多个引用更改/扩展/指向的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM