[Python-Dev] Re: PEP 322: Reverse Iteration
Alex Martelli
aleaxit at yahoo.com
Wed Nov 5 17:34:23 EST 2003
On Wednesday 05 November 2003 22:44, Samuele Pedroni wrote:
...
> > > I think he was wondering whether people rely on
> > >
> > > enumerate([1,2]).next
...which is one thing...
> > > i = enumerate([1,2])
> > > i is iter(i)
...which is another.
> >Ah, I see -- thanks! Well, in theory you COULD add a 'next' method too:
Note I specifically didn't say "make enumerate return an iterator" -- I said
"add a 'next' method". It's a non-special name (be that right or wrong) and
thus there is no prohibition against non-iterators having such a method.
> well, you would also get an iterator hybrid that violates:
No you wouldn't -- you would get a non-iterator type which exposes a
method named 'next', and that violates no Python rule.
> attach __reversed__ to an iterator, making enumerate just an iterable is
> not backward compatible but is a bit saner although it does not feel that
> natural either.
If anybody relies on that "i is iter(i)" then, yes. I have never seen that
relied upon. I _have_ seen quite a few cases of reliance on calls to a
'next' method to "throw the first item away" (no doubt a call to iter(...)
first would be preferable, but I'm just mentioning what I've seen).
I'm not sure supporting dubious "happens to work" existing usage is
_desirable_ -- I'm just saying it's _possible_ (in some cases, such as
this one) without necessarily violating anything.
Personally, since I found out that enumerate(reversed(x)) works almost
as well as reversed(enumerate(x)) [[or other hypotheticals -- such as
enumerate(x, reverse=True) OR reversed(x, enumerate=True)]], and
better than revrange(len(x)), for my use cases, I'm not particularly pro
NOR con wrt __reversed__ -- its pluses (which Raymond summarizes
quite well) and its minuses (Guido's worry about it promoting unwarranted
complications, my vague unease at "yet another special-case protocol
via a special-method when adaptation would handle it more uniformly")
are finely balanced. I just hope that, either with or without __reversed__,
reversed _does_ get in, at least, as Guido pointed out, tentatively (since
features, if need be, may be withdrawn before the beta phase).
Alex
More information about the Python-Dev
mailing list