Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12267

Re: how to format long if conditions

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 <python-python-list@m.gmane.org>
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" <cjw@ncf.ca>
Subject Re: how to format long if conditions
Date Sat, 27 Aug 2011 11:16:51 -0400
References <mailman.457.1314428909.27778.python-list@python.org> <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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.463.1314458229.27778.python-list@python.org> (permalink)
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

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

how to format long if conditions Arnaud Delobelle <arnodel@gmail.com> - 2011-08-27 08:08 +0100
  Re: how to format long if conditions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-27 17:24 +1000
    Re: how to format long if conditions Arnaud Delobelle <arnodel@gmail.com> - 2011-08-27 11:24 +0100
    Re: how to format long if conditions Ben Finney <ben+python@benfinney.id.au> - 2011-08-27 22:04 +1000
  Re: how to format long if conditions Hans Mulder <hansmu@xs4all.nl> - 2011-08-27 09:50 +0200
    Re: how to format long if conditions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-27 19:05 +1000
      Re: how to format long if conditions Hans Mulder <hansmu@xs4all.nl> - 2011-08-27 12:51 +0200
    Re: how to format long if conditions "Colin J. Williams" <cjw@ncf.ca> - 2011-08-27 11:16 -0400
      Re: how to format long if conditions Hans Mulder <hansmu@xs4all.nl> - 2011-08-27 17:53 +0200
        Re: how to format long if conditions "Colin J. Williams" <cjw@ncf.ca> - 2011-08-27 17:25 -0400
  Re: how to format long if conditions Roy Smith <roy@panix.com> - 2011-08-27 12:39 -0400

csiph-web