What's the difference between a reversed tuple and a reversed list?

前端 未结 2 750
隐瞒了意图╮
隐瞒了意图╮ 2021-01-01 14:05

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

>>> reversed((1,2))

>>> revers         


        
2条回答
  •  情歌与酒
    2021-01-01 14:27

    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().

提交回复
热议问题