Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!bcyclone03.am1.xlned.com!bcyclone03.am1.xlned.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'tree': 0.05; 'nested': 0.07; 'subject:test': 0.09; 'cc:addr:python-list': 0.11; "'0'": 0.16; '23,': 0.16; 'arithmetic.': 0.16; 'chained': 0.16; 'dict': 0.16; 'expressions,': 0.16; 'extensible': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'readability': 0.16; 'readable': 0.16; 'redundancy': 0.16; 'values:': 0.16; 'wrote:': 0.18; 'differ': 0.19; 'thu,': 0.19; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; "d'aprano": 0.31; 'steven': 0.31; "i'd": 0.34; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'turn': 0.37; 'two': 0.37; 'easily': 0.37; 'either': 0.39; 'most': 0.60; 'more': 0.64; 'techniques': 0.66; 'advantages': 0.68; 'subject::': 0.85; 'to:none': 0.92; 'technique': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=sae3EnlKOxeG6BG1qrQEKhJN1Rx8thc8xwIYuOzavlQ=; b=GNfzrJ4+0aQECz/vptuOw1kl9JMGJKjG8zbB+gqv3bNTkaFgWbe507XSWg2sz+XscZ 2aGRy2aQB98Cr6EHCWDfQ2/9x1gEVhFsnH2T8Jqg8H7J4fTLCyM/fsdGDfaKMfe9B3+J 4JnR0ns2IjUISniUaNiLNBVW5PucjtqNrlQCMp2Gk+OWfoCfQeEwETIvBjCMUtlmungw jcJyVigDy8q4FjP6Y7I/9HxusaS9ee66xVts2iDwgRpz2S02fKIhW0y2WqBKpvU7m0CQ eQ5ebkVf8OsghVMJD2CZzvXRqInP64QJhcS7kczynACWlQgXG4DPVNOIsCgU8FcojtxT mr8g== MIME-Version: 1.0 X-Received: by 10.107.18.151 with SMTP id 23mr3607166ios.27.1413996053621; Wed, 22 Oct 2014 09:40:53 -0700 (PDT) In-Reply-To: <5447db41$0$13000$c3e8da3$5496439d@news.astraweb.com> References: <54476af8$0$21651$426a74cc@news.free.fr> <7839376e-fc27-4299-ae63-4ddf17ef9a8a@googlegroups.com> <5447db41$0$13000$c3e8da3$5496439d@news.astraweb.com> Date: Thu, 23 Oct 2014 03:40:53 +1100 Subject: Re: (test) ? a:b From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1413996061 news.xs4all.nl 2848 [2001:888:2000:d::a6]:59321 X-Complaints-To: abuse@xs4all.nl X-Received-Body-CRC: 1419554671 X-Received-Bytes: 4268 Xref: csiph.com comp.lang.python:79458 On Thu, Oct 23, 2014 at 3:28 AM, Steven D'Aprano wrote: > It's also a technique easily extensible to more than two > values: > > '01TX'[n % 4] > > is in my opinion more readable than: > > i = n % 4 > '0' if i == 0 else '1' if i == 1 else 'T' if i == 3 else 'X' That's true when it's fundamentally arithmetic. But part of that readability difference is the redundancy in the second one. What if it weren't so redundant? 'Negative' if x < 0 else 'Low' if x < 10 else 'Mid' if x < 20 else 'High' You can't easily turn that into a dict lookup, nor indexing. It's either a chained if/elif tree or nested if/else expressions, which come to the same thing. So I'd say all the techniques have their advantages and disadvantages. The most important thing to note is where they differ in semantics, like the short-circuiting of if/else. ChrisA