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


Groups > comp.lang.python > #12261

Re: how to format long if conditions

From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: how to format long if conditions
References <mailman.457.1314428909.27778.python-list@python.org> <4e589b9b$0$29981$c3e8da3$5496439d@news.astraweb.com>
Date 2011-08-27 22:04 +1000
Message-ID <874o13ukl8.fsf@benfinney.id.au> (permalink)
Organization Unlimited download news at news.astraweb.com

Show all headers | View raw


Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes:

> I believe that PEP 8 now

Specifically the “Indentation” section contains::

    When using a hanging indent the following considerations should be
    applied; there should be no arguments on the first line and further
    indentation should be used to clearly distinguish itself as a
    continuation line.

> suggests something like this:
>
>         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)

That gives a SyntaxError. I think you mean one of these possible PEP 8
compliant forms::

    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)

or maybe::

    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)

> I consider that hideous

I think both of those (once modified to conform to both the Python
syntax and the PEP 8 guidelines) look clear and readable.

I mildy prefer the first for being a little more elegant, but the second
is slightly better for maintainability and reducing diff noise. Either
one makes me happy.

> and would prefer to write this:
>
>         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)

That one keeps tripping me up because the indentation doesn't make clear
where subordinate clauses begin and end. The (current) PEP 8 rules are
much better for readability in my eyes.


Having said that, I'm only a recent convert to the current PEP 8 style
for indentation of condition clauses. It took several heated arguments
with colleagues before I was able to admit the superiority of clear
indentation :-)

-- 
 \      “I am too firm in my consciousness of the marvelous to be ever |
  `\       fascinated by the mere supernatural …” —Joseph Conrad, _The |
_o__)                                                     Shadow-Line_ |
Ben Finney

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