Strange behaviour with reversed()
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Thu Oct 18 00:31:13 EDT 2007
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:
>>> L = list("abc")
>>> RL = reversed(L)
>>> del L
>>> list(RL)
['c', 'b', 'a']
This suggests that reversed() makes a copy of the list:
>>> L = list("abc")
>>> RL = reversed(L)
>>> L.append("d")
>>> list(RL)
['c', 'b', 'a']
This suggests that reversed() uses a reference to the original list:
>>> RL = reversed(L)
>>> L[0] = 'e'
>>> list(RL)
['d', 'c', 'b', 'e']
And these examples suggests that reversed() is confused, or at least
confusing:
>>> 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)
['b', 'a', 'd']
Does anyone know what reversed() is actually doing?
--
Steven.
More information about the Python-list
mailing list