20

Reversing a tuple and reversing a list returns objects of different type:

>>> reversed((1,2))
<reversed at 0x7fffe802f748>
>>> reversed([1,2])
<list_reverseiterator at 0x7fffebdd4400>

They have the same dir. Neither type is a subclass of the other.

Why is that? What can one do that the other can't?

4

2 回答 2

14

基本上,一个列表实现了该__reversed__方法并返回一个专门的对象,同时tuple回退到reversed任何序列的默认实现:

>>> list.__reversed__
<method '__reversed__' of 'list' objects>
>>> tuple.__reversed__
AttributeError: type object 'tuple' has no attribute '__reversed__'

现在,为什么列表不默认为序列reversed对象必须在列表对象本身的源代码中找到 - 可能它通过直接访问一些内部list属性来启用一些优化。

实际上看 C 代码,差别不大,当然也没有什么吸引眼球的地方——

我敢说特殊列表__reversed__实现是 Python2 天的遗留物, reversed 实际上将任何其他 Python 序列复制到 a list- 所以其他序列有特殊情况是没有意义的(当他们确实实现了一般enumreverse它时对于元组来说已经足够好了)。

我很确定,如果只是简单地注释掉 __reversed__上的插槽listobject.c,Python 及其列表就会像什么都没发生一样工作,默认为一般情况reversed

于 2016-10-20T23:07:37.087 回答
5

According to Python's documentation:

object.__reversed__(self)

Called (if present) by the reversed() built-in to implement reverse iteration. It should return a new iterator object that iterates over all the objects in the container in reverse order.

If the __reversed__() method is not provided, the reversed() built-in will fall back to using the sequence protocol (__len__() and __getitem__()). Objects that support the sequence protocol should only provide __reversed__() if they can provide an implementation that is more efficient than the one provided by reversed().

于 2016-10-20T23:12:42.480 回答