On 06.03.2016 19:53, Peter Otten wrote:
Sven R. Kunze wrote:

what's the reason that reversed(zip(...)) raises as a TypeError?

Would allowing reversed to handle zip and related functions lead to
strange errors?
In Python 3 zip() can deal with infinite iterables -- what would you expect
from

reversed(zip(count()))

?

Have I no idea. ;-)

But to me, "infinite" feels not like the most common use-case to most developers.

I just stumbled over it during rapid test case development:

for c in reversed(zip(ascii_lowercase, ascii_uppercase)):
    ...

Bam! That doesn't work although the code clearly describes what to do. :-(



If all arguments of zip() are finite and of equal length you can write

zip(reversed(a1), reversed(a2), ...)

or if you find that really useful something like

class myzip(zip):
...     def __init__(self, *args):
...         self.args = args
...     def __reversed__(self):
...         return zip(*(reversed(a) for a in self.args))
...
list(reversed(myzip("abc", [1,2,3])))
[('c', 3), ('b', 2), ('a', 1)]

While this might look right at first sight it really opens a can of worms.
First zip():

z = zip("abc", "def")
next(z)
('a', 'd')
list(z)
[('b', 'e'), ('c', 'f')]

Now myzip():

m = myzip("abc", "def")
next(m)
('a', 'd')
list(reversed(m))
[('c', 'f'), ('b', 'e'), ('a', 'd')]

Frankly, I have no idea what consistent behaviour should look like for a
zip() that can be "reverse-iterated".

PS: In Python 2 zip() would produce a list, so

list(reversed(zip("abc", "def")))
[('c', 'f'), ('b', 'e'), ('a', 'd')]

worked without requiring any code in zip().


--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to