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


Groups > comp.lang.python > #12305

Re: how to format long if conditions

From "Colin J. Williams" <cjw@ncf.ca>
Subject Re: how to format long if conditions
Date 2011-08-27 17:25 -0400
References <mailman.457.1314428909.27778.python-list@python.org> <4e58a1cc$0$2474$e4fe514c@news2.news.xs4all.nl> <mailman.463.1314458229.27778.python-list@python.org> <4e59130d$0$2411$e4fe514c@news2.news.xs4all.nl>
Newsgroups comp.lang.python
Message-ID <mailman.488.1314480331.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 27-Aug-11 11:53 AM, Hans Mulder wrote:
> On 27/08/11 17:16:51, Colin J. Williams wrote:
>
>> 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.
>
> That's a syntax error. You need to add parenthesis.
>
> How 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 PyBooleanAnd(left, right)
> )
>
> -- HansM

I like your 11:53 message but suggest indenting the if cond as below to 
make it clearer that it, with the preceding line, is all one statement.

Colin W.

#!/usr/bin/env python
z= 1
class PyCompare:
     complist = [True, False]
     def __init__(self):
         pass
left= PyCompare
right= PyCompare
def isinstance(a, b):
     return True
def PyBooleanAnd(a, b):
     return True
def PyCompare(a):
     return False
z=2

def try1():

       '''    Hans Mulder suggestion  03:50  '''
       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)

def try2():
       '''        cjw response - corrected  11:56  '''
       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 PyBooleanAnd(left, right))

def try3():
     '''       Hans Mulder 11:53   '''
     cond = (
         isinstance(left, PyCompare)
         and isinstance(right, PyCompare)
         and left.complist[-1] is right.complist[0]
         )  # not }
     py_and = (
              PyCompare(left.complist + right.complist[1:])
              if   cond
              else PyBooleanAnd(left, right)
             )
def main():
     try1()
     try2()
     try3()
if __name__ == '__main__':
     main()
     pass

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