Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: reversed(zip(...)) not working as intended Date: Sun, 06 Mar 2016 19:53:36 +0100 Organization: None Lines: 56 Message-ID: References: <56DC7727.1090001@mail.de> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de rt34FGatntDxz2BZW3x2KgSUSXCzKEkr/5ux8hL1i28A== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'args': 0.04; 'raises': 0.07; '*args):': 0.09; '3),': 0.09; 'finite': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'subject:not': 0.11; 'def': 0.13; "('b',": 0.16; "('c',": 0.16; '...)': 0.16; '2),': 0.16; 'iterables': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'reversed': 0.16; 'self.args': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'arguments': 0.22; 'consistent': 0.23; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'idea': 0.28; 'behaviour': 0.29; 'code': 0.30; 'skip:_ 10': 0.32; 'related': 0.32; 'useful': 0.33; 'class': 0.33; 'equal': 0.34; 'worked': 0.34; 'handle': 0.34; 'requiring': 0.35; 'something': 0.35; 'list,': 0.36; 'should': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'expect': 0.37; 'received:org': 0.37; 'skip:z 10': 0.38; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'strange': 0.63; 'sight': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8a9d.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104165 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())) ? 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().