
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? (Please `cc` any replies to me, I'm not getting mail from this list.) Ram.

On Mon, Apr 26, 2010 at 11:13 PM, Xavier Ho <contact@xavierho.com> wrote:
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? Ram.

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

On Mon, Apr 26, 2010 at 11:34 PM, George Sakkis <george.sakkis@gmail.com>wrote:
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.

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

On 28 April 2010 11:48, cool-RR <cool-rr@cool-rr.com> wrote:
Does anyone else care to express their opinion about the Reversable suggestion?
I think you need some more convincing use cases, and a better explanation of how you'd see it working. As things stand, it sounds like you're just suggesting it for completeness' sake. I assume you're expecting an implementation which checks if the type is an instance of Sequence or has a callable attribute __reversed__? Then again, I'm not a great user of ABCs in any case - easier to ask forgiveness and all that, I'd tend to just use reversed() and be prepared to deal with an error if the caller passed something non-reversible (maybe just by expecting the caller to deal with the exception, because they didn't satisfy the input requirements). Paul.

On Wed, Apr 28, 2010 at 1:00 PM, Paul Moore <p.f.moore@gmail.com> wrote:
I agree with your criticisms. I was indeed offering it for completeness' sake, I think that when I needed it, I had some iterable that I didn't know if I could use `reversed` on. 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.) Ram.

On Thu, Apr 29, 2010 at 1:01 PM, Paul Moore <p.f.moore@gmail.com> wrote:
Yes, I'm aware the implementation is simple. I was not asking for an implementation. I was just raising the idea that it should be part of the builtin `reversed`. But since there are no +1s, I guess not. Ram.

On 29 April 2010 12:22, cool-RR <cool-rr@cool-rr.com> wrote:
Sorry. My point was that the gains (in terms of the difficulty of implementing it yourself) are minor, so the cost doesn't justify it. Specifically, the costs are (1) extra complexity when describing the behaviour of reversed() and (2) more seriously, potential crashes if an infinite generator (for example) is passed to reversed() in error. Currently, reversed(infinite_gen) is a trappable error. With your proposal, it would halt the program until all memory was consumed, then crash. Anyone using a custom function such as my reversed_anything can be assumed to understand its limitations, whereas builtins can be assumed to behave "nicely". Looking at it the other way, it's easy to build reversed_anything given reversed, but it's not possible to build reversed given only reversed_anything. The builtin function should be the one that can be more easily used to build from. My apologies for being too terse. Paul.

On Fri, Apr 30, 2010 at 12:33 AM, Paul Moore <p.f.moore@gmail.com> wrote:
Thanks for the apology Paul. As I said in my message before, I proposed having an argument to `reversed` which will cause this "violent reverse" behavior. I didn't say I want `reversed` to do it by default. Like, I suggest you could do `reversed(iterator, violent=True)`, and only that will cause the iterator to be copied to a list before getting reversed. Though actually, I don't feel very strongly about this issue. I was just raising it to see whether it happened to be something that bothered other people as well, and then maybe it would have been worth doing something about. But it seems that it isn't, so I should probably let it go. Thanks for your attention, Ram.

On Mon, Apr 26, 2010 at 11:13 PM, Xavier Ho <contact@xavierho.com> wrote:
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? Ram.

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

On Mon, Apr 26, 2010 at 11:34 PM, George Sakkis <george.sakkis@gmail.com>wrote:
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.

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

On 28 April 2010 11:48, cool-RR <cool-rr@cool-rr.com> wrote:
Does anyone else care to express their opinion about the Reversable suggestion?
I think you need some more convincing use cases, and a better explanation of how you'd see it working. As things stand, it sounds like you're just suggesting it for completeness' sake. I assume you're expecting an implementation which checks if the type is an instance of Sequence or has a callable attribute __reversed__? Then again, I'm not a great user of ABCs in any case - easier to ask forgiveness and all that, I'd tend to just use reversed() and be prepared to deal with an error if the caller passed something non-reversible (maybe just by expecting the caller to deal with the exception, because they didn't satisfy the input requirements). Paul.

On Wed, Apr 28, 2010 at 1:00 PM, Paul Moore <p.f.moore@gmail.com> wrote:
I agree with your criticisms. I was indeed offering it for completeness' sake, I think that when I needed it, I had some iterable that I didn't know if I could use `reversed` on. 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.) Ram.

On Thu, Apr 29, 2010 at 1:01 PM, Paul Moore <p.f.moore@gmail.com> wrote:
Yes, I'm aware the implementation is simple. I was not asking for an implementation. I was just raising the idea that it should be part of the builtin `reversed`. But since there are no +1s, I guess not. Ram.

On 29 April 2010 12:22, cool-RR <cool-rr@cool-rr.com> wrote:
Sorry. My point was that the gains (in terms of the difficulty of implementing it yourself) are minor, so the cost doesn't justify it. Specifically, the costs are (1) extra complexity when describing the behaviour of reversed() and (2) more seriously, potential crashes if an infinite generator (for example) is passed to reversed() in error. Currently, reversed(infinite_gen) is a trappable error. With your proposal, it would halt the program until all memory was consumed, then crash. Anyone using a custom function such as my reversed_anything can be assumed to understand its limitations, whereas builtins can be assumed to behave "nicely". Looking at it the other way, it's easy to build reversed_anything given reversed, but it's not possible to build reversed given only reversed_anything. The builtin function should be the one that can be more easily used to build from. My apologies for being too terse. Paul.

On Fri, Apr 30, 2010 at 12:33 AM, Paul Moore <p.f.moore@gmail.com> wrote:
Thanks for the apology Paul. As I said in my message before, I proposed having an argument to `reversed` which will cause this "violent reverse" behavior. I didn't say I want `reversed` to do it by default. Like, I suggest you could do `reversed(iterator, violent=True)`, and only that will cause the iterator to be copied to a list before getting reversed. Though actually, I don't feel very strongly about this issue. I was just raising it to see whether it happened to be something that bothered other people as well, and then maybe it would have been worth doing something about. But it seems that it isn't, so I should probably let it go. Thanks for your attention, Ram.
participants (6)
-
Arnaud Delobelle
-
cool-RR
-
George Sakkis
-
Masklinn
-
Paul Moore
-
Xavier Ho