[Python-ideas] Reversable

cool-RR cool-rr at cool-rr.com
Mon Apr 26 23:41:24 CEST 2010


On Mon, Apr 26, 2010 at 11:34 PM, George Sakkis <george.sakkis at gmail.com>wrote:

> On Mon, Apr 26, 2010 at 11:18 PM, cool-RR <cool-rr at cool-rr.com> wrote:
> > On Mon, Apr 26, 2010 at 11:13 PM, Xavier Ho <contact at xavierho.com>
> wrote:
> >>
> >> On Tue, Apr 27, 2010 at 7:10 AM, cool-RR <cool-rr at cool-rr.com> wrote:
> >>>
> >>> Just something small that I thought of, and I haven't thought about
> this
> >>> deeply at all, so maybe this is way wrong. But: What about adding a
> >>> `Reversable` next to all the `Iterable` and `Container` and stuff?
> >>
> >> You mean there is something Iterable we can't reverse by doing [::-1] or
> >> calling reversed() ? This idea feels a bit too general to be useful. Any
> >> rationales?
> >>
> >> Cheers,
> >> Xav
> >
> > As far as I know, iterables are generally not reversable. Try defining a
> > simple iterator, like a class with just an `__iter__` function, and run
> > `reversed` on it. You get `TypeError: argument to reversed() must be a
> > sequence`. (Which by the way is a bad error message.)
> > Am I missing something?
>
> So what should reversed() (or a new Reversable()) return for, say,
> itertools.count() ?
>
> By the way, comp.lang.python [1] or the tutor mailing list [2] are
> more appropriate than python-ideas for asking
> questions "you haven't thought about deeply at all".
>
> George
>
> [1] http://mail.python.org/mailman/listinfo/python-list
> [2] http://mail.python.org/mailman/listinfo/tutor
>

I'm not really understanding you, George. Am I getting something very wrong
here? When you put `itertools.count()` into `reversed()`, you get an error,
like you should, because it's not a sequence and it doesn't define
`__reversed__`. So it's not a reversable object. I'm proposing to have a
`Reversable` similar to `Iterable`, which checks the existence of
`__reversed__` instead of `__iter__`.

Ram.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100426/b977028d/attachment.html>


More information about the Python-ideas mailing list