Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Oscar Benjamin Newsgroups: comp.lang.python Subject: Re: Simple exercise Date: Tue, 15 Mar 2016 11:09:15 +0000 Lines: 60 Message-ID: References: <88c5b5fa-66a0-461a-8ae4-b3264b32f679@googlegroups.com> <56e75076$0$22142$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de vELqpHJQnaWwYhv9RN9e9AilBWtpfyu6pkzW/ZQqq7LQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '(especially': 0.07; 'reason,': 0.07; 'cc:addr:python-list': 0.09; 'benjamin': 0.09; 'finite': 0.09; 'len(x)': 0.09; 'length.': 0.09; 'valueerror': 0.09; 'warn': 0.09; 'python': 0.10; 'index': 0.13; 'def': 0.13; '"zip"': 0.16; '2016': 0.16; 'cc:name:python list': 0.16; 'dropping': 0.16; 'infinite)': 0.16; 'iterables': 0.16; 'itertools': 0.16; 'length,': 0.16; 'loud': 0.16; 'object()': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'to:addr:pearwood.info': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.16; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'meant': 0.22; 'not,': 0.22; 'ones.': 0.22; 'pass': 0.22; 'am,': 0.23; '(or': 0.23; 'seems': 0.23; "python's": 0.23; 'header:In-Reply-To:1': 0.24; 'feature': 0.24; 'script': 0.25; 'error': 0.27; 'checking': 0.27; 'equivalent': 0.27; 'message-id:@mail.gmail.com': 0.27; 'sequence': 0.27; 'yield': 0.27; 'function': 0.28; 'values': 0.28; 'fine': 0.28; 'behaviour': 0.29; 'raise': 0.29; "i'm": 0.30; 'code': 0.30; 'strongly': 0.30; 'anyone': 0.32; 'common': 0.33; "d'aprano": 0.33; 'items.': 0.33; 'steven': 0.33; 'subject:Simple': 0.33; 'equal': 0.34; 'tue,': 0.34; 'received:google.com': 0.35; 'quite': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'instead': 0.36; 'there': 0.36; 'received:209.85': 0.36; '(and': 0.36; 'data.': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'johnson': 0.37; 'things': 0.38; 'skip:v 20': 0.38; 'received:209': 0.38; 'or,': 0.38; 'rather': 0.39; 'build': 0.40; 'where': 0.40; 'easy': 0.60; 'your': 0.60; 'avoid': 0.61; 'default': 0.61; 'more': 0.63; 'different': 0.63; 'march': 0.64; 'mar': 0.65; 'benefit': 0.66; 'better.': 0.66; 'intent': 0.66; 'matter.': 0.66; 'percent': 0.66; 'realise': 0.66; 'potentially': 0.67; 'situation': 0.67; 'obvious': 0.76; 'free:': 0.84; "it'd": 0.84; 'now)': 0.84; 'oscar': 0.84; 'absolutely': 0.88; 'am.': 0.91; 'opt-in': 0.91; 'write:': 0.91; 'rick': 0.93; 'hundred': 0.96 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=Y5mJ+bXmM9vSpfwncplL5n6Cqd/nTv+2NQadi4yUVnU=; b=lT0gaZ0+8WqecE1vboGmku6g35yaVAbWPLvrSqnNxyK58iJN/Kk5xGBTCLaxQYVmf/ M10VmOF1WuC7JypsBwCzGF/YRTC1oGBrMAYZjtgWBRxxK9qT9NC6RfUQZVIFDma8tovQ 6n1m1i3QJwbuz9TcDSfhIA4HlxkdyW2dFD+j+/3e4/NqfEfmqc0w8ZQ5ZPcEYOu0BS5r qu37p4ZEspNiUe9wswCxA7Gg0c84bq0X5S1uVv7/peUJdG19CQ9iez3/Skwhue+N67V3 6xcfY6BuvQ48eqEbAD9gjOg3eVryKTM2FuxK+rpq2dBmr8qcoQiARuXDKgs2uiIoAxNj X7PA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=Y5mJ+bXmM9vSpfwncplL5n6Cqd/nTv+2NQadi4yUVnU=; b=Kz75SrYKjeb1wOBCyWgaZ8AtI27GXSfBXVqLI9jU+sLgervsKo6+ER75O5K/pWzMBQ JbOestqh3K1dRv7tCceSBo+GD3VQTEOPMFTbqWJmt6JrMH728vceGefZlEvqox8PKVbs YAI/YNoniU5fG1iZP+0lEGeILDJxkUzNIZ56ivJYuAQmDu7jaDBmzyVpRqEtkhdHE970 fCQi86+TVrBREaN3yDfhg/I3VnvcjGPkDrtAm+pzykUpcIuZjr/TDv5fec7LMmiVS5bt Rpx5cK2NgNlY0wDPL9nvbZUAQSLcALN0CkwenTevCdY+tnrcEsX282n68FivOgZaTgmJ YFJw== X-Gm-Message-State: AD7BkJLacxxtr8Uhs3VKKk4gho4RLIoJaML7JGE2172PPmOoApu/k/NtIPWT2PosP1qLLVNJAUVMLd8d6WDhMw== X-Received: by 10.25.134.70 with SMTP id i67mr7371537lfd.90.1458040175200; Tue, 15 Mar 2016 04:09:35 -0700 (PDT) In-Reply-To: <56e75076$0$22142$c3e8da3$5496439d@news.astraweb.com> 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:104933 On 14 March 2016 at 23:59, Steven D'Aprano wrote: > On Tue, 15 Mar 2016 02:06 am, Oscar Benjamin wrote: > >> On 14 March 2016 at 14:35, Rick Johnson >> wrote: >>> >>> I would strongly warn anyone against using the zip function >>> unless >> ... >>> I meant to say: absolutely, one hundred percent *SURE*, that >>> both sequences are of the same length, or, absolutely one >>> hundred percent *SURE*, that dropping values is not going to >>> matter. For that reason, i avoid the zip function like the >>> plague. I would much rather get an index error, than let an >>> error pass silently. >> >> I also think it's unfortunate that zip silently discards items. > > Are you aware of itertools.zip_longest? I am. > That makes it easy to build a zip_strict: > > def zip_strict(*iterables): > pad = object() > for t in itertools.zip_longest(*iterables, fillvalue=pad): > if pad in t: > raise ValueError("iterables of different length") > yield t There are many ways to build a zipstrict. As I said in my own usage of zip I would almost always want it to raise an error because I almost always give zip iterables of the same length. However the situation where zipstrict would benefit is often the kind of situation where you're not really thinking about the fact that zip truncates. Also if you only have one zip call in a script it'd be easier (and clearer) to write: if len(x) != len(y): raise ValueError > Unfortunate or not, it seems to be quite common that "zip" (convolution) > discards items when sequences are of different lengths. I think the usual > intent is so that you can zip an infinite (or near infinite) sequence of > counters 1, 2, 3, 4, ... with the sequence you actually want, to get the > equivalent of Python's enumerate(). That's fine but in the Python code I see zip is much more often used with equal length finite iterables than with infinite ones. One of the things I like about Python (especially since I'm learning JS right now) that you can write your code in the obvious way and then most of your error checking comes for free: I want loud error messages instead of corrupted data. I would rather have the potentially bug-prone zip_shortest be an opt-in itertools feature and zip_strict the default behaviour for zip. I realise it's not going to change but I think it would be better. -- Oscar