Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'am,': 0.12; "'('": 0.16; "'if':": 0.16; 'indent': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.16; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'aligned': 0.23; 'formatting': 0.23; 'code': 0.25; "i'm": 0.27; 'separate': 0.28; 'bit': 0.28; 'cc:addr:python.org': 0.30; 'example': 0.30; 'subject:format': 0.30; 'break': 0.32; 'to:addr:python-list': 0.33; 'wondering': 0.33; "i've": 0.34; 'header:User-Agent:1': 0.34; 'do?': 0.34; 'header:X-Complaints-To:1': 0.35; 'statements': 0.37; 'received:75': 0.37; 'put': 0.37; 'but': 0.37; 'something': 0.37; 'received:org': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; "it's": 0.40; 'where': 0.40; 'sfxlen:4': 0.67; 'pfxlen:big': 0.77; "'then'": 0.84; 'right)': 0.84; 'ugly,': 0.84; 'subject:long': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Colin J. Williams" Subject: Re: how to format long if conditions Date: Sat, 27 Aug 2011 11:16:51 -0400 References: <4e58a1cc$0$2474$e4fe514c@news2.news.xs4all.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org X-Gmane-NNTP-Posting-Host: 75-119-254-164.dsl.teksavvy.com User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20110624 Thunderbird/5.0 In-Reply-To: <4e58a1cc$0$2474$e4fe514c@news2.news.xs4all.nl> 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314458229 news.xs4all.nl 2434 [2001:888:2000:d::a6]:44799 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12267 On 27-Aug-11 03:50 AM, Hans Mulder wrote: > On 27/08/11 09:08:20, Arnaud Delobelle wrote: >> 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: >> >> >> if (isinstance(left, PyCompare) and isinstance(right, PyCompare) >> and left.complist[-1] is right.complist[0]): >> py_and = PyCompare(left.complist + right.complist[1:]) >> else: >> py_and = PyBooleanAnd(left, right) >> >> What would you do? > > I would break after the '(' and indent the condition once and > put the '):' bit on a separate line, aligned with the 'if': > > > if ( > isinstance(left, PyCompare) > and isinstance(right, PyCompare) > and left.complist[-1] is right.complist[0] > ): > py_and = PyCompare(left.complist + right.complist[1:]) > else: > py_and = PyBooleanAnd(left, right) > > It may look ugly, but it's very clear where the condition part ends > and the 'then' part begins. > > -- HansM What about: cond= isinstance(left, PyCompare) and isinstance(right, PyCompare) and left.complist[-1] is right.complist[0] py_and= PyCompare(left.complist + right.complist[1:])if cond else: py_and = PyBooleanAnd(left, right) Colin W.