Arnaud said:
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)