Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!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.030 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'else:': 0.03; 'instance': 0.05; 'suppose': 0.05; 'pep': 0.07; 'suggestions.': 0.07; 'function:': 0.09; 'def': 0.15; '\xc2\xa0if': 0.16; 'cc:addr :python-list': 0.16; 'this:': 0.16; 'wrote:': 0.16; 'rewrite': 0.18; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.22; 'header:In- Reply-To:1': 0.22; 'formatting': 0.23; 'helper': 0.23; 'suggests': 0.23; 'code': 0.25; "i'm": 0.27; 'all,': 0.28; 'message- id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; 'example': 0.30; 'subject:format': 0.30; 'thanks': 0.30; 'class': 0.30; 'version': 0.32; 'wondering': 0.33; "i've": 0.34; '...': 0.34; 'do?': 0.34; 'statements': 0.37; 'thread': 0.37; 'but': 0.37; 'something': 0.37; 'two': 0.37; 'could': 0.38; 'steven': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'define': 0.39; 'believe': 0.65; 'details': 0.65; 'benefit': 0.66; 'as:': 0.70; 'august': 0.70; 'skip:\xc2 10': 0.74; 'exercise.': 0.84; 'hybrid': 0.84; 'other):': 0.84; 'right)': 0.84; 'received:209.85.218.46': 0.91; 'received:mail- yi0-f46.google.com': 0.91; 'subject:long': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=NS4RJC62jgCyuSLiHB8FzzYztln4uakERrOAVto/+54=; b=qdi/qfTKYoR1+Y0n4NMhZuGBHPbIQMc/kwWnlF6uy1p3iiDC0COBq7Dip/zA+h3vm3 6SHZ32C4kXGA5nvu447XZYgqjaZ05CjB7vkNM7wQgTbe7bgi4P7QwqoUiMHKfs/FN8KC Fme+oRnlywD2U6alA/Tc6pfPFb8a7NkBv0V9k= MIME-Version: 1.0 In-Reply-To: <4e589b9b$0$29981$c3e8da3$5496439d@news.astraweb.com> References: <4e589b9b$0$29981$c3e8da3$5496439d@news.astraweb.com> Date: Sat, 27 Aug 2011 11:24:33 +0100 Subject: Re: how to format long if conditions From: Arnaud Delobelle To: "Steven D'Aprano" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 118 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314440676 news.xs4all.nl 2464 [2001:888:2000:d::a6]:40468 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12258 On 27 August 2011 08:24, Steven D'Aprano wrote: > Arnaud Delobelle wrote: > >> Hi all, >> >> I'm wondering what advice you have about formatting if statements with >> long conditions (I always format my code to <80 colums) >> >> Here's an example taken from something I'm writing at the moment and >> how I've formatted it: >> >> >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (isinstance(left, PyCompare) and isinstan= ce(right, PyCompare) >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 and left.complis= t[-1] is right.complist[0]): >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 py_and =3D PyCompare(left.comp= list + right.complist[1:]) >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 else: >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 py_and =3D PyBooleanAnd(left, = right) >> >> What would you do? > > I believe that PEP 8 now suggests something like this: > > =C2=A0 =C2=A0 =C2=A0 =C2=A0if ( > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0isinstance(left, P= yCompare) and isinstance(right, PyCompare) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0and left.complist[= -1] is right.complist[0]): > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0py_and =3D PyCompare(left.compli= st + right.complist[1:] > =C2=A0 =C2=A0 =C2=A0 =C2=A0else: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0py_and =3D PyBooleanAnd(left, ri= ght) > > > I consider that hideous and would prefer to write this: > > > =C2=A0 =C2=A0 =C2=A0 =C2=A0if (isinstance(left, PyCompare) and isinstance= (right, PyCompare) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0and left.complist[-1] is right.c= omplist[0]): > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0py_and =3D PyCompare(left.compli= st + right.complist[1:] > =C2=A0 =C2=A0 =C2=A0 =C2=A0else: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0py_and =3D PyBooleanAnd(left, ri= ght) > > > Or even this: > > =C2=A0 =C2=A0 =C2=A0 =C2=A0tmp =3D ( > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0isinstance(left, PyCompare) and = isinstance(right, PyCompare) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0and left.complist[-1] is right.c= omplist[0]) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0) > =C2=A0 =C2=A0 =C2=A0 =C2=A0if tmp: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0py_and =3D PyCompare(left.compli= st + right.complist[1:] > =C2=A0 =C2=A0 =C2=A0 =C2=A0else: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0py_and =3D PyBooleanAnd(left, ri= ght) > > > But perhaps the best solution is to define a helper function: > > def is_next(left, right): > =C2=A0 =C2=A0"""Returns True if right is the next PyCompare to left.""" > =C2=A0 =C2=A0return (isinstance(left, PyCompare) and isinstance(right, Py= Compare) > =C2=A0 =C2=A0 =C2=A0 =C2=A0and left.complist[-1] is right.complist[0]) > =C2=A0 =C2=A0# PEP 8 version left as an exercise. > > > # later... > =C2=A0 =C2=A0 =C2=A0 =C2=A0if is_next(left, right): > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0py_and =3D PyCompare(left.compli= st + right.complist[1:] > =C2=A0 =C2=A0 =C2=A0 =C2=A0else: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0py_and =3D PyBooleanAnd(left, ri= ght) > Thanks Steven and Hans for you suggestions. For this particular instance I've decided to go for a hybrid approach: * Add two methods to PyCompare: class PyCompare(PyExpr): ... def extends(self, other): if not isinstance(other, PyCompare): return False else: return self.complist[0] =3D=3D other.complist[-1] def chain(self, other): return PyCompare(self.complist + other.complist[1:]) * Rewrite the if as: if isinstance(right, PyCompare) and right.extends(left): py_and =3D left.chain(right) else: py_and =3D PyBooleanAnd(left, right) The additional benefit is to hide the implementation details of PyCompare (I suppose this could illustrate the thread on when to create functions). --=20 Arnaud