[Python-ideas] Reversable

Paul Moore p.f.moore at gmail.com
Thu Apr 29 13:01:03 CEST 2010


On 29 April 2010 02:15, cool-RR <cool-rr at cool-rr.com> wrote:
> Though this leads me to another thought: Maybe we should have an argument to
> `reversed` which will instruct it to make a list out of the iterable and
> then return the `reversed` of that list, but only when the original iterable
> is not naturally reversible. This way you could be sure that `reversed` will
> work on any iterable. (When you don't care much about performance, of
> course.)

def reversed_anything(a):
    try:
        return reversed(a)
    except TypeError:
        return reversed(list(a))

Paul



More information about the Python-ideas mailing list