Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'operator': 0.03; 'argument': 0.05; 'true,': 0.05; 'subject:How': 0.10; 'cc:addr :python-list': 0.11; '"or"': 0.16; '1969': 0.16; 'argument.': 0.16; 'defaulting': 0.16; 'finney': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'pythonic': 0.16; 'quirks': 0.16; 'rather,': 0.16; 'value;': 0.16; 'wrote:': 0.18; 'example': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'string,': 0.24; 'cc:2**0': 0.24; 'second': 0.26; 'header:In- Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'assumes': 0.31; 'relies': 0.31; 'text': 0.33; 'url:python': 0.33; 'beginning': 0.33; 'definition': 0.35; 'received:google.com': 0.35; 'subject:?': 0.36; 'url:org': 0.36; 'wrong': 0.37; 'ben': 0.38; 'pm,': 0.38; 'anything': 0.39; 'subject:can': 0.39; 'blank': 0.60; 'year.': 0.61; 'url:3': 0.61; 'first': 0.61; 'default': 0.69; 'subject:this': 0.83; 'expressive': 0.84; 'url:reference': 0.84; 'to:none': 0.92 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:content-transfer-encoding; bh=WBf/qH5GBG81Fldc7ppAsuoujvbNkkys5vv04nvecwU=; b=KW5xG4QP17PZrmY7vJRokFLGbHzkH6adZPSbobT8dMvx17FIDt8zl71mNLfRaZgBH3 ZOQPIrjc/F+WhRWwENs1EASLLJ8rn7wo3wfo8uCnjFpyL+N4KJvtmwQn2k+t2HiCNpWC scU9WZjgY8MY9IE3jfnyT4aoHMqgCxzeJB+RrEbF7sKgLiHY/E7mPi4Mpl2jBKupZFpg o0gJFTyFMfcXmE+T73CvPbS8DVHGADEo1r9ZkTHyAj0E6ar8ZwtsX5DPLoff5DYRVwSt GPsB+dvLpniJish0+p3UOMCrL6O1fJ4rm2qCFenJXBxefJ5iJED9Qros9Ak0uezbV9SF /PqQ== MIME-Version: 1.0 X-Received: by 10.66.243.70 with SMTP id ww6mr3684017pac.47.1396773143540; Sun, 06 Apr 2014 01:32:23 -0700 (PDT) In-Reply-To: <85lhvi27qh.fsf@benfinney.id.au> References: <85zjjz141k.fsf@benfinney.id.au> <985B907F-8EC7-4B5C-8A9F-64AEC3E5803D@gmail.com> <85lhvi27qh.fsf@benfinney.id.au> Date: Sun, 6 Apr 2014 18:32:23 +1000 Subject: Re: How can I parse this correctly? From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1396773151 news.xs4all.nl 2853 [2001:888:2000:d::a6]:48889 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69758 On Sun, Apr 6, 2014 at 6:16 PM, Ben Finney wro= te: >> print int(row['YEAR'] or 0000) > > =E2=80=9Cfoo or bar=E2=80=9D is not a Pythonic way to get a default value= ; it relies on > quirks of implementation and is not expressive as to the meaning you > intend. > > Rather, be explicit: > > # Default to the beginning of the project. > year =3D 1969 > if row['YEAR']: > # Use the value as a text representation of a year. > year =3D int(row['YEAR']) What's wrong with "foo or bar" as a means of defaulting a blank string? (Obviously this assumes you know you have a string, as it'll equally default a 0 or a 0.0 or a [] or anything else false.) The definition of the "or" operator is that it returns its first argument if it's true, otherwise it returns its second argument. That's not a quirk of implementation. This exact example (with strings, no less!) can be found in help("or") and in the docs: https://docs.python.org/3/reference/expressions.html#boolean-operations ChrisA