Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #12258
| References | <mailman.457.1314428909.27778.python-list@python.org> <4e589b9b$0$29981$c3e8da3$5496439d@news.astraweb.com> |
|---|---|
| Date | 2011-08-27 11:24 +0100 |
| Subject | Re: how to format long if conditions |
| From | Arnaud Delobelle <arnodel@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.459.1314440676.27778.python-list@python.org> (permalink) |
On 27 August 2011 08:24, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> 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:
>>
>>
>> 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 believe that PEP 8 now 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)
>
>
> I consider that hideous 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)
>
>
> Or even this:
>
> tmp = (
> isinstance(left, PyCompare) and isinstance(right, PyCompare)
> and left.complist[-1] is right.complist[0])
> )
> if tmp:
> py_and = PyCompare(left.complist + right.complist[1:]
> else:
> py_and = PyBooleanAnd(left, right)
>
>
> But perhaps the best solution is to define a helper function:
>
> def is_next(left, right):
> """Returns True if right is the next PyCompare to left."""
> return (isinstance(left, PyCompare) and isinstance(right, PyCompare)
> and left.complist[-1] is right.complist[0])
> # PEP 8 version left as an exercise.
>
>
> # later...
> if is_next(left, right):
> py_and = PyCompare(left.complist + right.complist[1:]
> else:
> py_and = PyBooleanAnd(left, right)
>
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] == 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 = left.chain(right)
else:
py_and = 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).
--
Arnaud
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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