[Python-ideas] Reversable

George Sakkis george.sakkis at gmail.com
Tue Apr 27 00:12:17 CEST 2010


On Mon, Apr 26, 2010 at 11:41 PM, cool-RR <cool-rr at cool-rr.com> wrote:
> 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__`.

Sorry, it wasn't obvious (to me at least) that you were talking about
the abstract classes under the collections module, and in your second
post you implied (again, that was my understanding at any rate) that
defining a class with just an __iter__ should be acceptable by
`reversed` instead of raising a TypeError.

On the other hand a collections.Reversable abstract class that checks
specifically for __reversed__ sounds quite reasonable.

George



More information about the Python-ideas mailing list