This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: implement __reversed__ on reversed types
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: jason.curtis, rhettinger, steven.daprano
Priority: normal Keywords:

Created on 2019-08-05 22:40 by jason.curtis, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg349085 - (view) Author: Jason Curtis (jason.curtis) * Date: 2019-08-05 22:40
I've just been trying to implement some logic which potentially involves reversing things back to their initial orders, and it'd be nice to just be able to call reversed() on something that has already been reversed.

>>> reversed(reversed([1,2,3,4]))
TypeError: 'list_reverseiterator' object is not reversible

Seems like this should be trivial to implement by just returning the initial iterator.

Happy to post a pull request if it would be considered.
msg349088 - (view) Author: Jason Curtis (jason.curtis) * Date: 2019-08-05 22:52
Ok, not so sure about the PR now; I dug around and realized this is a C implementation and my C is likely not strong enough!
msg349095 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-08-06 01:53
Both 3.7 and 3.8 are in feature-freeze, so the earliest we can get this would be 3.9.

But before that, we have to decide on what reversing a reversed object means.

it = reversed([1, 2, 3, 4])
next(it)
list( reversed(it) )

Should that give [1, 2, 3] or [1, 2, 3, 4]? I think that either answer will annoy and surprise some people.

[1, 2, 3] will surprise those who expect to get the original iterable back. [1, 2, 3, 4] will surprise those who expect reversed to operate on the current state of an iterator, not it's previous state.
msg349098 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-08-06 04:35
> Happy to post a pull request if it would be considered.

I don't we should do this.  Mostly, it seems pointless, but it also isn't really something we would want people to be doing.

> Should that give [1, 2, 3] or [1, 2, 3, 4]? I think that
> either answer will annoy and surprise some people.

Right.  I don't think we should open this can of worms.

Thank for you the suggestion, but we should decline this one unless a compelling use case arises.
History
Date User Action Args
2022-04-11 14:59:18adminsetgithub: 81951
2019-08-06 04:35:37rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg349098

resolution: rejected
stage: resolved
2019-08-06 01:53:49steven.dapranosetnosy: + steven.daprano

messages: + msg349095
versions: + Python 3.9, - Python 3.7
2019-08-05 22:54:43jason.curtissettitle: reversed class should implement __reversed__ -> implement __reversed__ on reversed types
2019-08-05 22:52:16jason.curtissetmessages: + msg349088
2019-08-05 22:40:03jason.curtiscreate