Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.06; 'nested': 0.07; 'lookup': 0.09; 'occasionally': 0.09; 'python': 0.11; '70,': 0.16; 'abbreviated': 0.16; 'blocks': 0.16; "can't.": 0.16; 'dictionaries': 0.16; 'dictionary.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'mapping,': 0.16; 'mappings,': 0.16; 'normally,': 0.16; 'sense,': 0.16; 'subject:user': 0.16; 'tuple': 0.16; 'weapon': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'bit': 0.19; 'normally': 0.19; 'translated': 0.19; 'starts': 0.20; 'handles': 0.22; 'creating': 0.23; 'entries': 0.24; 'instance,': 0.24; 'string,': 0.24; 'mon,': 0.24; 'tables': 0.26; 'header:In-Reply-To:1': 0.27; 'array': 0.29; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; '(which': 0.31; 'code': 0.31; 'second,': 0.31; 'though.': 0.31; 'table': 0.34; 'maybe': 0.34; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'data,': 0.36; 'subject:New': 0.37; 'so,': 0.37; 'easily': 0.37; 'nov': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'rather': 0.38; 'ability': 0.39; 'to:addr:python.org': 0.39; 'subject: / ': 0.60; 'most': 0.60; 'full': 0.61; "you're": 0.61; 'first': 0.61; "you'll": 0.62; 'such': 0.63; 'more': 0.64; 'statement,': 0.68; 'obvious': 0.74; 'special': 0.74; 'million': 0.74; 'day': 0.76; "it'd": 0.84; '2013': 0.98 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:to :content-type; bh=LM50NzupJ5cgrWIRYDMEXMum4x1PJjZGnD8nS4Mo9+A=; b=Wzq2Fbli/LYu/IN0uCCNOQucmS6SLVwRkorkakeJR/xMvnTKUlpJIkX4wz/V4uf6+1 JcxttHIe6F8UFFQtE+V81C/gPnRS0raff90OnBbBrxi/6jJ9KOarxb+EO65EsBLqzb9e cYC3Fw0qkyD6YE6TjWPaYGGpUaFqpT9sTpSe+jnYlKs3qmK/BCM7jp1W5MBbEe9M6C3F I/wkWef47Sc/OWltBfs3NAEPNQ5+rO05LDKCYaeffPo+5pxI+4zPpLMSkSIk1dV53pXI +5ZuEnC28CDdXFPgylb35QS8v4DHOe/vahUwYMNv5iqTbbksodmI0baL0yDrXfvWgQWa +wZA== MIME-Version: 1.0 X-Received: by 10.68.65.199 with SMTP id z7mr29712797pbs.26.1384166367499; Mon, 11 Nov 2013 02:39:27 -0800 (PST) In-Reply-To: <1c4c0901-f80a-42f3-9df5-7e7431353079@googlegroups.com> References: <-JadnUirYuhUruPPnZ2dnUVZ8rSdnZ2d@bt.com> <1c4c0901-f80a-42f3-9df5-7e7431353079@googlegroups.com> Date: Mon, 11 Nov 2013 21:39:27 +1100 Subject: Re: New user's initial thoughts / criticisms of Python From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384166376 news.xs4all.nl 15871 [2001:888:2000:d::a6]:32884 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59061 On Mon, Nov 11, 2013 at 9:09 PM, wrote: > Regarding the "select" statement, I think the most "Pythonic" approach is using dictionaries rather than nested ifs. > Supposing we want to decode abbreviated day names ("mon") to full names ("Monday"): That's an obvious mapping, though. If you're using a select/switch statement to handle straight-forward one-to-one mappings, then yes, obviously the better way to do it is to use a dictionary. In the more general sense, a switch/case block is much more directly translated into if/elif/else statements. You can't, for instance, build up a dictionary that handles inequalities, but you can do that with elif. That is, normally you can't. I have occasionally built up mappings that handle inequalities - it's a form of denormalization. Consider the following logic: A 'minor weapon' is based on a roll of a 100-sided dice. If it's 01 to 70, "+1 weapon: 2,000gp [weapon]"; if it's 71 to 85, "+2 weapon: 8,000gp [weapon]"; if 86 to 90, "Specific weapon [minor specific weapon]"; and if 91 to 100, "Special ability [minor special weapon] and roll again". My code to handle that starts out with this array: "minor weapon":({ 70,"+1 weapon: 2,000gp [weapon]", 85,"+2 weapon: 8,000gp [weapon]", 90,"Specific weapon [minor specific weapon]", 100,"Special ability [minor special weapon] and roll again", }), (that's Pike; in Python it'd be a list, or maybe a tuple of tuples), and denormalizes it into a lookup table by creating 70 entries quoting the first string, 15 quoting the second, 5, and 10, respectively. So, with a bit of preprocessing, a lookup table (which in this case is an array (list), but could just as easily be a dict) can be used to handle inequalities. But this is because lookup tables can be treated as data, where if/elif/else blocks have to be code; there are roughly 42 million such lookup tables in the code I snagged that from, and having code for each one would work out far less manageable. Normally, you'll want to render inequalities with code as if/elif. ChrisA