Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #104170

Re: reversed(zip(...)) not working as intended

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From "Sven R. Kunze" <srkunze@mail.de>
Newsgroups comp.lang.python
Subject Re: reversed(zip(...)) not working as intended
Date Sun, 6 Mar 2016 20:52:38 +0100
Lines 71
Message-ID <mailman.0.1457293962.2226.python-list@python.org> (permalink)
References <56DC7727.1090001@mail.de> <nbhubk$6cn$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de aLWh4va4P3effOKG7i74uwlMrGc4HOjTKNQnjO3rSgvQ==
Return-Path <srkunze@mail.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'args': 0.04; ':-(': 0.07; 'raises': 0.07; '*args):': 0.09; '3),': 0.09; 'finite': 0.09; 'python': 0.10; 'subject:not': 0.11; 'def': 0.13; "('b',": 0.16; "('c',": 0.16; '...)': 0.16; '2),': 0.16; 'development:': 0.16; 'iterables': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reversed': 0.16; 'self.args': 0.16; 'wrote:': 0.16; ';-)': 0.18; 'do.': 0.22; 'arguments': 0.22; 'developers.': 0.22; 'consistent': 0.23; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'idea': 0.28; 'behaviour': 0.29; 'code': 0.30; 'skip:_ 10': 0.32; 'related': 0.32; 'useful': 0.33; 'class': 0.33; 'common': 0.33; 'equal': 0.34; 'worked': 0.34; 'handle': 0.34; 'received:10.0': 0.34; 'requiring': 0.35; 'something': 0.35; 'but': 0.36; 'list,': 0.36; 'should': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'really': 0.37; 'expect': 0.37; 'skip:z 10': 0.38; 'test': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'charset:windows-1252': 0.62; 'strange': 0.63; 'otten': 0.84; 'sight': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/simple; d=mail.de; s=mail201212; t=1457293959; bh=VUPQXO7OjK1yZKHdV45yrJR9uyDzEDAlosl4OEQzY+o=; h=Subject:To:References:From:Date:In-Reply-To:From; b=w8scvsoigQo7sZDqHYEO6VWqSV9nLuEcqTF6aQkQfHRfjwzZjdgTf1YWh4v8q1ec4 eB7ZFkv+P69BdgeYh71pLiaq9B1QOaStGkTmdHa0m/sgppzwBtncD1DdtlxLjoGsuk Ux9btBQGgRmM/xDWFitzF/iw31BkQNMJkMLUjMYY=
In-Reply-To <nbhubk$6cn$1@ger.gmane.org>
X-purgate clean
X-purgate This mail is considered clean (visit http://www.eleven.de for further information)
X-purgate-type clean
X-purgate-Ad Categorized by eleven eXpurgate (R) http://www.eleven.de
X-purgate This mail is considered clean (visit http://www.eleven.de for further information)
X-purgate clean
X-purgate-size 1789
X-purgate-ID 154282::1457293959-00004575-C7506CF7/0/0
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:104170

Show key headers only | View raw


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().
>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: reversed(zip(...)) not working as intended "Sven R. Kunze" <srkunze@mail.de> - 2016-03-06 20:52 +0100

csiph-web