S
Steven D'Aprano
I don't understand how reversed() is operating. I've read the description
in the docs:
reversed(seq)
Return a reverse iterator. seq must be an object which supports the
sequence protocol (the __len__() method and the __getitem__() method with
integer arguments starting at 0). New in version 2.4.
and help(reversed) but neither gives any insight to what happens when you
use reversed() on a sequence, then modify the sequence.
This works as I expected:
['c', 'b', 'a']
This suggests that reversed() makes a copy of the list:
['c', 'b', 'a']
This suggests that reversed() uses a reference to the original list:
And these examples suggests that reversed() is confused, or at least
confusing:
Does anyone know what reversed() is actually doing?
in the docs:
reversed(seq)
Return a reverse iterator. seq must be an object which supports the
sequence protocol (the __len__() method and the __getitem__() method with
integer arguments starting at 0). New in version 2.4.
and help(reversed) but neither gives any insight to what happens when you
use reversed() on a sequence, then modify the sequence.
This works as I expected:
['c', 'b', 'a']
This suggests that reversed() makes a copy of the list:
['c', 'b', 'a']
This suggests that reversed() uses a reference to the original list:
['d', 'c', 'b', 'e']RL = reversed(L)
L[0] = 'e'
list(RL)
And these examples suggests that reversed() is confused, or at least
confusing:
['b', 'a', 'd'][]RL = reversed(L)
del L[2]
list(RL)
L = list("abc")
RL = reversed(L)
L.insert(0, "d")
L ['d', 'a', 'b', 'c']
list(RL)
Does anyone know what reversed() is actually doing?