FAQ
I have a question about best practices when it comes to line wrapping/
continuation and indentation, specifically in the case of an if
statement.

When I write an if statement with many conditions, I prefer to use a
parenthesis around the whole block and get the implicit continuation,
rather than ending each line with an escape character. Thus, using
the example from the style guide (http://www.python.org/dev/peps/
pep-0008/) I would write:

if (width == 0 and
height == 0 and
color == 'red' and
emphasis == 'strong' or
highlight > 100):
raise ValueError("sorry, you lose")

The problem should be obvious -- it's not easy to see where the
conditional ends and the statement begins since they have the same
indentation. Part of the problem, I suppose, is that Emacs indents
'height' and the other lines in the conditional to 4 spaces (because
of the parenthesis). How do people deal with this situation?

Thanks,
Henrik

Search Discussions

  • Xavier Ho at May 27, 2010 at 12:48 pm

    On 27 May 2010 22:22, HH wrote:

    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100):
    raise ValueError("sorry, you lose")
    I've gotta say - I've bumped into this problem before, and I'm sure many
    other have - this is a valid question. It just hasn't bothered me enough to
    ask...

    Correct me if I'm wrong, but I think the following is equivalent, and looks
    better. Although this won't fix all ugly cases in that problem.

    if (width, height, color, emphasis) == (0, 0, 'red', 'strong') or highlight
    100:
    raise ValueError("sorry, you lose")

    Cheers,
    Xav
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20100527/aa389897/attachment.html>
  • Martin P. Hellwig at May 27, 2010 at 12:54 pm

    On 05/27/10 13:22, HH wrote:
    I have a question about best practices when it comes to line wrapping/
    continuation and indentation, specifically in the case of an if
    statement.

    When I write an if statement with many conditions, I prefer to use a
    parenthesis around the whole block and get the implicit continuation,
    rather than ending each line with an escape character. Thus, using
    the example from the style guide (http://www.python.org/dev/peps/
    pep-0008/) I would write:

    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100):
    raise ValueError("sorry, you lose")

    The problem should be obvious -- it's not easy to see where the
    conditional ends and the statement begins since they have the same
    indentation. Part of the problem, I suppose, is that Emacs indents
    'height' and the other lines in the conditional to 4 spaces (because
    of the parenthesis). How do people deal with this situation?

    Thanks,
    Henrik
    Well style guide aside (if pylint is happy with it, so am I) it depends
    on what I want to emphasize.

    For example if it is really one long line with every item in it being
    equally important I do this:
    if width == 0 and height == 0 and color == 'red' and emphasis ==
    'strong' \
    or
    highlight > 100:
    raise ValueError("sorry, you lose")

    In case it doesn't display correctly, I break up the line to nearest
    80th character and align the remaining part on the next line to the
    right to the 80th character.

    If I want to emphasize visually a certain part I would do something like
    this:

    if width == 0 and height == 0 and color == 'red' \
    and emphasis == 'strong' or highlight > 100:
    raise ValueError("sorry, you lose")

    But these are my preference, and since it is most likely that I have to
    read again what I have written I write it in a way that it is most
    readable to me within the constraints of pylint.

    --
    mph
  • Jean-Michel Pichavant at May 27, 2010 at 12:57 pm

    HH wrote:
    I have a question about best practices when it comes to line wrapping/
    continuation and indentation, specifically in the case of an if
    statement.

    When I write an if statement with many conditions, I prefer to use a
    parenthesis around the whole block and get the implicit continuation,
    rather than ending each line with an escape character. Thus, using
    the example from the style guide (http://www.python.org/dev/peps/
    pep-0008/) I would write:

    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100):
    raise ValueError("sorry, you lose")

    The problem should be obvious -- it's not easy to see where the
    conditional ends and the statement begins since they have the same
    indentation. Part of the problem, I suppose, is that Emacs indents
    'height' and the other lines in the conditional to 4 spaces (because
    of the parenthesis). How do people deal with this situation?

    Thanks,
    Henrik
    One possible solution

    if (
    width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100
    ):
    raise ValueError("sorry, you lose")


    JM
  • Xavier Ho at May 27, 2010 at 1:14 pm

    On 27 May 2010 22:57, Jean-Michel Pichavant wrote:

    One possible solution

    if (
    width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100
    ):
    raise ValueError("sorry, you lose")

    But... but you have a sad face in one line! :(
    Good trick.

    -Xav
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20100527/3f577e13/attachment.html>
  • Xavier Ho at May 27, 2010 at 1:26 pm

    On 27 May 2010 22:57, Jean-Michel Pichavant wrote:

    One possible solution
    if (
    width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100
    ):
    raise ValueError("sorry, you lose")
    Oh, one minor optimisation. You can put the last condition first:

    if (
    highlight > 100 or
    width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong'
    ):
    raise ValueError("sorry, you lose")
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20100527/e77ced48/attachment.html>
  • Xavier Ho at May 27, 2010 at 1:35 pm

    On 27 May 2010 23:26, Xavier Ho wrote:

    Oh, one minor optimisation. You can put the last condition first
    I take that back. You really can't, without using more parans or ifs.
    Apologies.

    -Xav
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20100527/50a9b313/attachment.html>
  • Tim Chase at May 27, 2010 at 2:10 pm

    On 05/27/2010 07:22 AM, HH wrote:
    When I write an if statement with many conditions, I prefer to use a
    parenthesis around the whole block and get the implicit continuation,
    rather than ending each line with an escape character. Thus, using
    the example from the style guide (http://www.python.org/dev/peps/
    pep-0008/) I would write:

    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100):
    raise ValueError("sorry, you lose")
    While it's not PEP material, I tend to use the coding standards I
    learned working for Computer Sciences Corporation (10 yrs ago, so
    things may have changed) that mandated 2 levels of indentation
    for continued lines, turning the above into

    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100):
    # or the closing "):" on this line,
    # aligned with the previous line
    raise ValueError("sorry, you lose")

    which is fairly close to Jean-Michel's proposal.

    -tkc
  • MRAB at May 27, 2010 at 2:32 pm

    HH wrote:
    I have a question about best practices when it comes to line wrapping/
    continuation and indentation, specifically in the case of an if
    statement.

    When I write an if statement with many conditions, I prefer to use a
    parenthesis around the whole block and get the implicit continuation,
    rather than ending each line with an escape character. Thus, using
    the example from the style guide (http://www.python.org/dev/peps/
    pep-0008/) I would write:

    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100):
    raise ValueError("sorry, you lose")

    The problem should be obvious -- it's not easy to see where the
    conditional ends and the statement begins since they have the same
    indentation. Part of the problem, I suppose, is that Emacs indents
    'height' and the other lines in the conditional to 4 spaces (because
    of the parenthesis). How do people deal with this situation?
    I would probably use half-indentation:

    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100):
    raise ValueError("sorry, you lose")

    Try doing that with tabs! :-)
  • Jean-Michel Pichavant at May 27, 2010 at 3:46 pm

    MRAB wrote:
    HH wrote:
    I have a question about best practices when it comes to line wrapping/
    continuation and indentation, specifically in the case of an if
    statement.

    When I write an if statement with many conditions, I prefer to use a
    parenthesis around the whole block and get the implicit continuation,
    rather than ending each line with an escape character. Thus, using
    the example from the style guide (http://www.python.org/dev/peps/
    pep-0008/) I would write:

    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100):
    raise ValueError("sorry, you lose")

    The problem should be obvious -- it's not easy to see where the
    conditional ends and the statement begins since they have the same
    indentation. Part of the problem, I suppose, is that Emacs indents
    'height' and the other lines in the conditional to 4 spaces (because
    of the parenthesis). How do people deal with this situation?
    I would probably use half-indentation:

    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100):
    raise ValueError("sorry, you lose")

    Try doing that with tabs! :-)
    if (width ==0 and
    ' \\ // ' and height == 0 and
    ' \\// ' and color == 'red' and
    ' /OO\ ' and emphasis == 'strong' and
    ' \></ ' or highlight > 100):
    raise ValueError("sorry, you lose")

    Try doing this with spaces !! :p

    JM
  • Harald Luessen at May 27, 2010 at 3:34 pm

    On Thu, 27 May 2010 HH wrote:
    I have a question about best practices when it comes to line wrapping/
    continuation and indentation, specifically in the case of an if
    statement.

    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100):
    raise ValueError("sorry, you lose")
    My solution would probably look like this:

    if ( width == 0
    and height == 0
    and color == 'red'
    and emphasis == 'strong'
    or highlight > 100
    ):
    raise ValueError("sorry, you lose")

    Harald
  • Alain Ketterlin at May 27, 2010 at 3:37 pm

    HH <henrikho at gmail.com> writes:

    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100):
    raise ValueError("sorry, you lose")
    I prefer to see the "and" at the beginning of continuation lines, and
    usually group related items. I never mix operators without putting
    explicit parentheses. Something like:

    if ( width == 0 and height == 0
    and color == 'red'
    and ( emphasis == 'strong' or highlight > 100 ) ):
    raise ValueError("sorry, you lose")

    Anyway, choose your style and stick to it.
    The problem should be obvious -- it's not easy to see where the
    conditional ends and the statement begins since they have the same
    indentation. Part of the problem, I suppose, is that Emacs indents
    'height' and the other lines in the conditional to 4 spaces (because
    of the parenthesis).
    Emacs aligns "height" with "width", not with the parenthesis. You can
    put as many spaces as you want before "(" or between "(" and "width",
    and the following lines will follow. At least that's what happens with
    my stock emacs-snapshot on ubuntu, with python.el.

    If you use a backslashes at the end of line, emacs will double-indent
    the following lines, but I think you said you prefer paretheses...

    -- Alain.
  • HH at May 27, 2010 at 4:45 pm

    On May 27, 11:37?am, Alain Ketterlin wrote:
    HH <henri... at gmail.com> writes:
    ? ? if (width == 0 and
    ? ? ? ? height == 0 and
    ? ? ? ? color == 'red' and
    ? ? ? ? emphasis == 'strong' or
    ? ? ? ? highlight > 100):
    ? ? ? ? raise ValueError("sorry, you lose")
    I prefer to see the "and" at the beginning of continuation lines, and
    usually group related items. I never mix operators without putting
    explicit parentheses. Something like:

    ? ? ?if ?( ?width == 0 and height == 0
    ? ? ? ? ? ? and color == 'red'
    ? ? ? ? ? ? and ( emphasis == 'strong' or highlight > 100 ) ):
    ? ? ? ? ?raise ValueError("sorry, you lose")
    Thanks for all suggestions! I like this solution a lot.

    I agree with your statement about mixed operators and explicit
    parentheses -- expecially since the eye/brain parser often does it
    wrong, and I believe you demonstrated that above...

    In [29]: print(False and (False or True))
    False
    In [30]: print(False and False or True)
    True
    If you use a backslashes at the end of line, emacs will double-indent
    the following lines, but I think you said you prefer paretheses...
    Yes, I much prefer the parentheses -- partly because PEP-8 suggests it
    but mostly because the escapes get lost too easily.


    HH
  • Jonathan Hartley at May 28, 2010 at 9:54 am

    On May 27, 1:57?pm, Jean-Michel Pichavant wrote:
    HH wrote:
    I have a question about best practices when it comes to line wrapping/
    continuation and indentation, specifically in the case of an if
    statement.
    When I write an if statement with many conditions, I prefer to use a
    parenthesis around the whole block and get the implicit continuation,
    rather than ending each line with an escape character. ?Thus, using
    the example from the style guide (http://www.python.org/dev/peps/
    pep-0008/) I would write:
    ? ? if (width == 0 and
    ? ? ? ? height == 0 and
    ? ? ? ? color == 'red' and
    ? ? ? ? emphasis == 'strong' or
    ? ? ? ? highlight > 100):
    ? ? ? ? raise ValueError("sorry, you lose")
    The problem should be obvious -- it's not easy to see where the
    conditional ends and the statement begins since they have the same
    indentation. ?Part of the problem, I suppose, is that Emacs indents
    'height' and the other lines in the conditional to 4 spaces (because
    of the parenthesis). ?How do people deal with this situation?
    Thanks,
    Henrik
    One possible solution

    ? ? if (
    ? ? ? ? ? ? width == 0 and
    ? ? ? ? ? ? height == 0 and
    ? ? ? ? ? ? color == 'red' and
    ? ? ? ? ? ? emphasis == 'strong' or
    ? ? ? ? ? ? highlight > 100
    ? ? ? ?):
    ? ? ? ? raise ValueError("sorry, you lose")

    JM ?
    I've always liked this, or even:

    if (
    width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100
    ):
    raise ValueError("sorry, you lose")


    but my co-workers have uniformly gone bananas whenever I try it.
  • Jean-Michel Pichavant at May 28, 2010 at 10:34 am

    Jonathan Hartley wrote:
    On May 27, 1:57 pm, Jean-Michel Pichavant wrote:

    HH wrote:
    I have a question about best practices when it comes to line wrapping/
    continuation and indentation, specifically in the case of an if
    statement.

    When I write an if statement with many conditions, I prefer to use a
    parenthesis around the whole block and get the implicit continuation,
    rather than ending each line with an escape character. Thus, using
    the example from the style guide (http://www.python.org/dev/peps/
    pep-0008/) I would write:

    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100):
    raise ValueError("sorry, you lose")

    The problem should be obvious -- it's not easy to see where the
    conditional ends and the statement begins since they have the same
    indentation. Part of the problem, I suppose, is that Emacs indents
    'height' and the other lines in the conditional to 4 spaces (because
    of the parenthesis). How do people deal with this situation?

    Thanks,
    Henrik
    One possible solution

    if (
    width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100
    ):
    raise ValueError("sorry, you lose")

    JM
    I've always liked this, or even:

    if (
    width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100
    ):
    raise ValueError("sorry, you lose")


    but my co-workers have uniformly gone bananas whenever I try it.
    I tried to give a layout that fits the OP way of doing, but I would not
    use what I described above, so I can understand why your co workers go
    bananas :)

    when it comes to extended conditions in if statement I prefer to write
    something like

    if self.haveLost():
    raise ValueError("sorry, you lose")

    It drastically improves the reading because it splits the notion of what
    to do in which case, and how do you identify the cases (i.e. what should
    I do when I've lost, and how do I know that I've lost). If you don't
    want to pollute your upper namespace you can embed the function that way:

    def foo():
    width = 0
    height = 0
    color = 'red'
    emphasis = 'strong'

    def haveLost():
    return not width and not height and color == 'red' and emphasis
    =='strong'

    if haveLost():
    raise ValueError("sorry you lose")

    It has the cool side effect to name your condition as well, that helps
    debugging the condition *a lot*.

    JM
  • Jonathan Hartley at May 28, 2010 at 10:48 am

    On 28/05/2010 11:34, Jean-Michel Pichavant wrote:
    Jonathan Hartley wrote:
    On May 27, 1:57 pm, Jean-Michel Pichavant <jeanmic... at sequans.com>
    wrote:
    HH wrote:
    I have a question about best practices when it comes to line wrapping/
    continuation and indentation, specifically in the case of an if
    statement.
    When I write an if statement with many conditions, I prefer
    to use a
    parenthesis around the whole block and get the implicit continuation,
    rather than ending each line with an escape character. Thus, using
    the example from the style guide (http://www.python.org/dev/peps/
    pep-0008/) I would write:
    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100):
    raise ValueError("sorry, you lose")
    The problem should be obvious -- it's not easy to see where the
    conditional ends and the statement begins since they have the same
    indentation. Part of the problem, I suppose, is that Emacs indents
    'height' and the other lines in the conditional to 4 spaces (because
    of the parenthesis). How do people deal with this situation?
    Thanks,
    Henrik
    One possible solution

    if (
    width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100
    ):
    raise ValueError("sorry, you lose")

    JM
    I've always liked this, or even:

    if (
    width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100
    ):
    raise ValueError("sorry, you lose")


    but my co-workers have uniformly gone bananas whenever I try it.
    I tried to give a layout that fits the OP way of doing, but I would
    not use what I described above, so I can understand why your co
    workers go bananas :)

    when it comes to extended conditions in if statement I prefer to write
    something like

    if self.haveLost():
    raise ValueError("sorry, you lose")

    It drastically improves the reading
    Good point.

    +1 for naming the condition, hooray for self-documenting code.

    Sometime last year at my workplace, we started referring to comments as
    'lies', we now always try to use techniques like this instead of comments.

    --
    Jonathan Hartley Made of meat. http://tartley.com
    tartley at tartley.com +44 7737 062 225 twitter/skype: tartley
  • Colin J. Williams at May 28, 2010 at 5:37 pm

    On 28-May-10 05:54 AM, Jonathan Hartley wrote:
    On May 27, 1:57 pm, Jean-Michel Pichavantwrote:
    HH wrote:
    I have a question about best practices when it comes to line wrapping/
    continuation and indentation, specifically in the case of an if
    statement.
    When I write an if statement with many conditions, I prefer to use a
    parenthesis around the whole block and get the implicit continuation,
    rather than ending each line with an escape character. Thus, using
    the example from the style guide (http://www.python.org/dev/peps/
    pep-0008/) I would write:
    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100):
    raise ValueError("sorry, you lose")
    The problem should be obvious -- it's not easy to see where the
    conditional ends and the statement begins since they have the same
    indentation. Part of the problem, I suppose, is that Emacs indents
    'height' and the other lines in the conditional to 4 spaces (because
    of the parenthesis). How do people deal with this situation?
    Thanks,
    Henrik
    One possible solution

    if (
    width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100
    ):
    raise ValueError("sorry, you lose")

    JM
    I've always liked this, or even:

    if (
    width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100
    ):
    raise ValueError("sorry, you lose")


    but my co-workers have uniformly gone bananas whenever I try it. I liked:
    On 27-May-10 08:48 AM, Xavier Ho wrote:
    On 27 May 2010 22:22, HH <henrikho at gmail.com
    wrote: >
    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100):
    raise ValueError("sorry, you lose")
    >
    >
    I've gotta say - I've bumped into this problem before, and I'm sure many
    other have - this is a valid question. It just hasn't bothered me enough
    to ask... >
    Correct me if I'm wrong, but I think the following is equivalent, and
    looks better. Although this won't fix all ugly cases in that problem.. >
    if (width, height, color, emphasis) == (0, 0, 'red', 'strong') or
    highlight > 100:
    raise ValueError("sorry, you lose") >
    Cheers,
    Xav
    but nobody commented.

    Colin W.
  • John at May 30, 2010 at 12:23 am

    On May 28, 10:37?am, "Colin J. Williams" wrote:
    On 28-May-10 05:54 AM, Jonathan Hartley wrote:

    On May 27, 1:57 pm, Jean-Michel Pichavant<jeanmic... at sequans.com>
    wrote:
    HH wrote:
    I have a question about best practices when it comes to line wrapping/
    continuation and indentation, specifically in the case of an if
    statement.
    When I write an if statement with many conditions, I prefer to use a
    parenthesis around the whole block and get the implicit continuation,
    rather than ending each line with an escape character. ?Thus, using
    the example from the style guide (http://www.python.org/dev/peps/
    pep-0008/) I would write:
    ? ? ?if (width == 0 and
    ? ? ? ? ?height == 0 and
    ? ? ? ? ?color == 'red' and
    ? ? ? ? ?emphasis == 'strong' or
    ? ? ? ? ?highlight> ?100):
    ? ? ? ? ?raise ValueError("sorry, you lose")
    The problem should be obvious -- it's not easy to see where the
    conditional ends and the statement begins since they have the same
    indentation. ?Part of the problem, I suppose, is that Emacs indents
    'height' and the other lines in the conditional to 4 spaces (because
    of the parenthesis). ?How do people deal with this situation?
    Thanks,
    Henrik
    One possible solution
    ? ? ?if (
    ? ? ? ? ? ? ?width == 0 and
    ? ? ? ? ? ? ?height == 0 and
    ? ? ? ? ? ? ?color == 'red' and
    ? ? ? ? ? ? ?emphasis == 'strong' or
    ? ? ? ? ? ? ?highlight> ?100
    ? ? ? ? ):
    ? ? ? ? ?raise ValueError("sorry, you lose")
    JM
    I've always liked this, or even:
    ? ?if (
    ? ? ? ?width == 0 and
    ? ? ? ?height == 0 and
    ? ? ? ?color == 'red' and
    ? ? ? ?emphasis == 'strong' or
    ? ? ? ?highlight> ?100
    ? ?):
    ? ? ? ?raise ValueError("sorry, you lose")
    but my co-workers have uniformly gone bananas whenever I try it.
    I liked:

    On 27-May-10 08:48 AM, Xavier Ho wrote:
    ?> On 27 May 2010 22:22, HH <henri... at gmail.com?> wrote:

    ?>
    ?> ? ? ? ? if (width == 0 and
    ?> ? ? ? ? ? ? height == 0 and
    ?> ? ? ? ? ? ? color == 'red' and
    ?> ? ? ? ? ? ? emphasis == 'strong' or
    ?> ? ? ? ? ? ? highlight > 100):
    ?> ? ? ? ? ? ? raise ValueError("sorry, you lose")
    ?>
    ?>
    ?> I've gotta say - I've bumped into this problem before, and I'm sure many
    ?> other have - this is a valid question. It just hasn't bothered me enough
    ?> to ask...
    ?>
    ?> Correct me if I'm wrong, but I think the following is equivalent, and
    ?> looks better. Although this won't fix all ugly cases in that problem..
    ?>
    ?> if (width, height, color, emphasis) == (0, 0, 'red', 'strong') or
    ?> highlight > 100:
    ?> ? ? ?raise ValueError("sorry, you lose")
    ?>
    ?> Cheers,
    ?> Xav

    but nobody commented.

    Colin W.
    Colin:
    Sure, you can do it that way. IMO, though, the OP was wrong, and so
    is the PEP. Source code is meant to communicate. So it must transmit
    the correct information to the computer; it also must inform your
    coworkers. That means that you have a responsibility to care what
    they think, though you privately have your opinions. Another reason
    the PEP is faulty in this circumstance is that a misplaced backslash,
    or a missing one, is easily found and fixed. A misplaced parentheses,
    or just one of a pair, will transform your source code into something
    which may compile and then give faulty results: a disaster.
    So keep it simple, and make it legible.
    Yours,
    John
  • Mark Lawrence at May 30, 2010 at 12:59 am

    On 30/05/2010 01:23, john wrote:
    On May 28, 10:37 am, "Colin J. Williams"wrote:
    On 28-May-10 05:54 AM, Jonathan Hartley wrote:

    On May 27, 1:57 pm, Jean-Michel Pichavant<jeanmic... at sequans.com>
    wrote:
    HH wrote:
    I have a question about best practices when it comes to line wrapping/
    continuation and indentation, specifically in the case of an if
    statement.
    When I write an if statement with many conditions, I prefer to use a
    parenthesis around the whole block and get the implicit continuation,
    rather than ending each line with an escape character. Thus, using
    the example from the style guide (http://www.python.org/dev/peps/
    pep-0008/) I would write:
    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100):
    raise ValueError("sorry, you lose")
    The problem should be obvious -- it's not easy to see where the
    conditional ends and the statement begins since they have the same
    indentation. Part of the problem, I suppose, is that Emacs indents
    'height' and the other lines in the conditional to 4 spaces (because
    of the parenthesis). How do people deal with this situation?
    Thanks,
    Henrik
    One possible solution
    if (
    width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100
    ):
    raise ValueError("sorry, you lose")
    JM
    I've always liked this, or even:
    if (
    width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100
    ):
    raise ValueError("sorry, you lose")
    but my co-workers have uniformly gone bananas whenever I try it. I liked:
    On 27-May-10 08:48 AM, Xavier Ho wrote:
    On 27 May 2010 22:22, HH<henri... at gmail.com> wrote:
    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100):
    raise ValueError("sorry, you lose")


    I've gotta say - I've bumped into this problem before, and I'm sure many
    other have - this is a valid question. It just hasn't bothered me enough
    to ask...

    Correct me if I'm wrong, but I think the following is equivalent, and
    looks better. Although this won't fix all ugly cases in that problem..

    if (width, height, color, emphasis) == (0, 0, 'red', 'strong') or
    highlight> 100:
    raise ValueError("sorry, you lose")

    Cheers,
    Xav
    but nobody commented.

    Colin W.
    Colin:
    Sure, you can do it that way. IMO, though, the OP was wrong, and so
    is the PEP. Source code is meant to communicate. So it must transmit
    the correct information to the computer; it also must inform your
    coworkers. That means that you have a responsibility to care what
    they think, though you privately have your opinions. Another reason
    the PEP is faulty in this circumstance is that a misplaced backslash,
    or a missing one, is easily found and fixed. A misplaced parentheses,
    or just one of a pair, will transform your source code into something
    which may compile and then give faulty results: a disaster.
    So keep it simple, and make it legible.
    Yours,
    John
    IMHO complete garbage, if your editor doesn't show misplaced or missing
    parenthesis by highlighting you're using the wrong editor :)

    Regards.

    Mark Lawrence.
  • Nathan Rice at May 30, 2010 at 5:50 am
    I prefer to just break such things into multiple lines. You're doing that
    already anyhow, it's not much of a speed hit, and it makes exactly what
    you're testing explicit. If I break a statement onto multiple lines I only
    use parenthesis, and that is as a last resort. In my opinion there's almost
    always some combination of variable assignments and lambda expressions that
    uses fewer lines and is clearer.

    is_correct_style = width == 0 and height == 0 and color == 'red'
    if (is_correct_style and emphasis == 'strong') or highlight > 100:

    On Sat, May 29, 2010 at 8:59 PM, Mark Lawrence wrote:
    On 30/05/2010 01:23, john wrote:

    On May 28, 10:37 am, "Colin J. Williams"<cjwilliam... at gmail.com>
    wrote:
    On 28-May-10 05:54 AM, Jonathan Hartley wrote:

    On May 27, 1:57 pm, Jean-Michel Pichavant<jeanmic... at sequans.com>
    wrote:
    HH wrote:
    I have a question about best practices when it comes to line wrapping/
    continuation and indentation, specifically in the case of an if
    statement.
    When I write an if statement with many conditions, I prefer to use a
    parenthesis around the whole block and get the implicit continuation,
    rather than ending each line with an escape character. Thus, using
    the example from the style guide (http://www.python.org/dev/peps/
    pep-0008/) I would write:
    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100):
    raise ValueError("sorry, you lose")
    The problem should be obvious -- it's not easy to see where the
    conditional ends and the statement begins since they have the same
    indentation. Part of the problem, I suppose, is that Emacs indents
    'height' and the other lines in the conditional to 4 spaces (because
    of the parenthesis). How do people deal with this situation?
    Thanks,
    Henrik
    One possible solution
    if (
    width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100
    ):
    raise ValueError("sorry, you lose")
    JM
    I've always liked this, or even:
    if (
    width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100
    ):
    raise ValueError("sorry, you lose")
    but my co-workers have uniformly gone bananas whenever I try it.
    I liked:
    On 27-May-10 08:48 AM, Xavier Ho wrote:
    On 27 May 2010 22:22, HH<henri... at gmail.com> <mailto:
    henri... at gmail.com>> wrote:
    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100):
    raise ValueError("sorry, you lose")


    I've gotta say - I've bumped into this problem before, and I'm sure many
    other have - this is a valid question. It just hasn't bothered me enough
    to ask...

    Correct me if I'm wrong, but I think the following is equivalent, and
    looks better. Although this won't fix all ugly cases in that problem..
    if (width, height, color, emphasis) == (0, 0, 'red', 'strong') or
    highlight> 100:
    raise ValueError("sorry, you lose")

    Cheers,
    Xav
    but nobody commented.

    Colin W.
    Colin:
    Sure, you can do it that way. IMO, though, the OP was wrong, and so
    is the PEP. Source code is meant to communicate. So it must transmit
    the correct information to the computer; it also must inform your
    coworkers. That means that you have a responsibility to care what
    they think, though you privately have your opinions. Another reason
    the PEP is faulty in this circumstance is that a misplaced backslash,
    or a missing one, is easily found and fixed. A misplaced parentheses,
    or just one of a pair, will transform your source code into something
    which may compile and then give faulty results: a disaster.
    So keep it simple, and make it legible.
    Yours,
    John
    IMHO complete garbage, if your editor doesn't show misplaced or missing
    parenthesis by highlighting you're using the wrong editor :)

    Regards.

    Mark Lawrence.



    --
    http://mail.python.org/mailman/listinfo/python-list
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20100530/395fbdf8/attachment.html>
  • Colin J. Williams at May 30, 2010 at 7:10 pm

    On 30-May-10 01:50 AM, Nathan Rice wrote:
    I prefer to just break such things into multiple lines. You're doing
    that already anyhow, it's not much of a speed hit, and it makes exactly
    what you're testing explicit. If I break a statement onto multiple
    lines I only use parenthesis, and that is as a last resort. In my
    opinion there's almost always some combination of variable assignments
    and lambda expressions that uses fewer lines and is clearer.

    is_correct_style = width == 0 and height == 0 and color == 'red'
    if (is_correct_style and emphasis == 'strong') or highlight > 100:


    On Sat, May 29, 2010 at 8:59 PM, Mark Lawrence <breamoreboy at yahoo.co.uk
    wrote:

    On 30/05/2010 01:23, john wrote:

    On May 28, 10:37 am, "Colin J. Williams"<cjwilliam... at gmail.com
    wrote:

    On 28-May-10 05:54 AM, Jonathan Hartley wrote:

    On May 27, 1:57 pm, Jean-Michel
    Pichavant<jeanmic... at sequans.com
    wrote:

    HH wrote:

    I have a question about best practices when it
    comes to line wrapping/
    continuation and indentation, specifically in
    the case of an if
    statement.


    When I write an if statement with many
    conditions, I prefer to use a
    parenthesis around the whole block and get the
    implicit continuation,
    rather than ending each line with an escape
    character. Thus, using
    the example from the style guide
    (http://www.python.org/dev/peps/
    pep-0008/) I would write:


    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100):
    raise ValueError("sorry, you lose")


    The problem should be obvious -- it's not easy
    to see where the
    conditional ends and the statement begins since
    they have the same
    indentation. Part of the problem, I suppose, is
    that Emacs indents
    'height' and the other lines in the conditional
    to 4 spaces (because
    of the parenthesis). How do people deal with
    this situation?


    Thanks,
    Henrik


    One possible solution


    if (
    width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100
    ):
    raise ValueError("sorry, you lose")


    JM


    I've always liked this, or even:


    if (
    width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100
    ):
    raise ValueError("sorry, you lose")


    but my co-workers have uniformly gone bananas whenever I
    try it.


    I liked:
    On 27-May-10 08:48 AM, Xavier Ho wrote:
    On 27 May 2010 22:22, HH<henri... at gmail.com
    <mailto:henri... at gmail.com>> <mailto:henri... at gmail.com
    wrote:
    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100):
    raise ValueError("sorry, you lose")


    I've gotta say - I've bumped into this problem before,
    and I'm sure many
    other have - this is a valid question. It just hasn't
    bothered me enough
    to ask...

    Correct me if I'm wrong, but I think the following is
    equivalent, and
    looks better. Although this won't fix all ugly cases in
    that problem..
    if (width, height, color, emphasis) == (0, 0, 'red',
    'strong') or
    highlight> 100:
    raise ValueError("sorry, you lose")

    Cheers,
    Xav
    but nobody commented.

    Colin W.


    Colin:
    Sure, you can do it that way. IMO, though, the OP was wrong,
    and so
    is the PEP. Source code is meant to communicate. So it must
    transmit
    the correct information to the computer; it also must inform your
    coworkers. That means that you have a responsibility to care what
    they think, though you privately have your opinions. Another reason
    the PEP is faulty in this circumstance is that a misplaced
    backslash,
    or a missing one, is easily found and fixed. A misplaced
    parentheses,
    or just one of a pair, will transform your source code into
    something
    which may compile and then give faulty results: a disaster.
    So keep it simple, and make it legible.
    Yours,
    John


    IMHO complete garbage, if your editor doesn't show misplaced or
    missing parenthesis by highlighting you're using the wrong editor :)

    Regards.

    Mark Lawrence.



    --
    http://mail.python.org/mailman/listinfo/python-list
    Perhaps the arrangement below shows the matching a little better than
    the Xav suggestion. The main point is that, to me, the tuple shows the
    item by item matching better than a series of and clauses:

    # tif.py

    (width, height, color, emphasis)= 0, 0, 'red', 'strong'
    highlight= 99
    if (width, height, color, emphasis) == \
    ( 0, 0, 'red', 'strong') or highlight> 100:
    raise ValueError("sorry, you lose")

    Colin W.
  • Lie Ryan at May 31, 2010 at 9:15 pm

    On 05/31/10 05:10, Colin J. Williams wrote:
    On 30-May-10 01:50 AM, Nathan Rice wrote:
    On 27-May-10 08:48 AM, Xavier Ho wrote:
    On 27 May 2010 22:22, HH<henri... at gmail.com
    <mailto:henri... at gmail.com>> <mailto:henri... at gmail.com
    wrote:
    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100):
    raise ValueError("sorry, you lose")


    I've gotta say - I've bumped into this problem before,
    and I'm sure many
    other have - this is a valid question. It just hasn't
    bothered me enough
    to ask...

    Correct me if I'm wrong, but I think the following is
    equivalent, and
    looks better. Although this won't fix all ugly cases in
    that problem..
    if (width, height, color, emphasis) == (0, 0, 'red',
    'strong') or
    highlight> 100:
    raise ValueError("sorry, you lose")

    Cheers,
    Xav
    but nobody commented.

    Colin W.


    Colin:
    Sure, you can do it that way. IMO, though, the OP was wrong,
    and so
    is the PEP. Source code is meant to communicate. So it must
    transmit
    the correct information to the computer; it also must inform your
    coworkers. That means that you have a responsibility to care
    what
    they think, though you privately have your opinions. Another
    reason
    the PEP is faulty in this circumstance is that a misplaced
    backslash,
    or a missing one, is easily found and fixed. A misplaced
    parentheses,
    or just one of a pair, will transform your source code into
    something
    which may compile and then give faulty results: a disaster.
    So keep it simple, and make it legible.
    Yours,
    John


    IMHO complete garbage, if your editor doesn't show misplaced or
    missing parenthesis by highlighting you're using the wrong editor :)
    Perhaps the arrangement below shows the matching a little better than
    the Xav suggestion. The main point is that, to me, the tuple shows the
    item by item matching better than a series of and clauses:

    # tif.py

    (width, height, color, emphasis)= 0, 0, 'red', 'strong'
    highlight= 99
    if (width, height, color, emphasis) == \
    ( 0, 0, 'red', 'strong') or highlight> 100:
    raise ValueError("sorry, you lose")
    How about:

    all(a == b for a,b in ((width, 0), (height, 0), (color, 'red'),
    (emphasis, 'strong'))))
  • Colin J. Williams at May 31, 2010 at 11:34 pm

    On 31-May-10 17:15 PM, Lie Ryan wrote:
    On 05/31/10 05:10, Colin J. Williams wrote:
    On 30-May-10 01:50 AM, Nathan Rice wrote:
    On 27-May-10 08:48 AM, Xavier Ho wrote:
    On 27 May 2010 22:22, HH<henri... at gmail.com
    <mailto:henri... at gmail.com>> <mailto:henri... at gmail.com
    wrote:
    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100):
    raise ValueError("sorry, you lose")


    I've gotta say - I've bumped into this problem before,
    and I'm sure many
    other have - this is a valid question. It just hasn't
    bothered me enough
    to ask...

    Correct me if I'm wrong, but I think the following is
    equivalent, and
    looks better. Although this won't fix all ugly cases in
    that problem..
    if (width, height, color, emphasis) == (0, 0, 'red',
    'strong') or
    highlight> 100:
    raise ValueError("sorry, you lose")

    Cheers,
    Xav
    but nobody commented.

    Colin W.


    Colin:
    Sure, you can do it that way. IMO, though, the OP was wrong,
    and so
    is the PEP. Source code is meant to communicate. So it must
    transmit
    the correct information to the computer; it also must inform your
    coworkers. That means that you have a responsibility to care
    what
    they think, though you privately have your opinions. Another
    reason
    the PEP is faulty in this circumstance is that a misplaced
    backslash,
    or a missing one, is easily found and fixed. A misplaced
    parentheses,
    or just one of a pair, will transform your source code into
    something
    which may compile and then give faulty results: a disaster.
    So keep it simple, and make it legible.
    Yours,
    John


    IMHO complete garbage, if your editor doesn't show misplaced or
    missing parenthesis by highlighting you're using the wrong editor :)
    Perhaps the arrangement below shows the matching a little better than
    the Xav suggestion. The main point is that, to me, the tuple shows the
    item by item matching better than a series of and clauses:

    # tif.py

    (width, height, color, emphasis)= 0, 0, 'red', 'strong'
    highlight= 99
    if (width, height, color, emphasis) == \
    ( 0, 0, 'red', 'strong') or highlight> 100:
    raise ValueError("sorry, you lose")
    How about:

    all(a == b for a,b in ((width, 0), (height, 0), (color, 'red'),
    (emphasis, 'strong'))))
    You need to add "or highlight > 100"
  • Giacomo Boffi at Jun 4, 2010 at 9:22 am

    HH <henrikho at gmail.com> writes:

    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100):
    raise ValueError("sorry, you lose")

    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight > 100):

    raise ValueError("sorry, you lose")

    --
    compro mobili vecchi - vendo mobili antichi
  • Aahz at Jun 4, 2010 at 9:25 pm
    In article <mailman.663.1274969462.32709.python-list at python.org>,
    Tim Chase wrote:
    While it's not PEP material, I tend to use the coding standards I
    learned working for Computer Sciences Corporation (10 yrs ago, so
    things may have changed) that mandated 2 levels of indentation
    for continued lines, turning the above into

    if (width == 0 and
    height == 0 and
    color == 'red' and
    emphasis == 'strong' or
    highlight> 100):
    # or the closing "):" on this line,
    # aligned with the previous line
    raise ValueError("sorry, you lose")
    +1
    --
    Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/

    "If you don't know what your program is supposed to do, you'd better not
    start writing it." --Dijkstra
  • Greg Couch at Jun 8, 2010 at 5:36 am

    On May 27, 5:22?am, HH wrote:
    I have a question about best practices when it comes to line wrapping/
    continuation and indentation, specifically in the case of an if
    statement.

    When I write an if statement with many conditions, I prefer to use a
    parenthesis around the whole block and get the implicit continuation,
    rather than ending each line with an escape character. ?Thus, using
    the example from the style guide (http://www.python.org/dev/peps/
    pep-0008/) I would write:

    ? ? if (width == 0 and
    ? ? ? ? height == 0 and
    ? ? ? ? color == 'red' and
    ? ? ? ? emphasis == 'strong' or
    ? ? ? ? highlight > 100):
    ? ? ? ? raise ValueError("sorry, you lose")

    The problem should be obvious -- it's not easy to see where the
    conditional ends and the statement begins since they have the same
    indentation. ?Part of the problem, I suppose, is that Emacs indents
    'height' and the other lines in the conditional to 4 spaces (because
    of the parenthesis). ?How do people deal with this situation?

    Thanks,
    Henrik
    To show another alternative, I like:

    if (width == 0
    and height == 0
    and color == 'red'
    and emphasis == 'strong'
    or highlight > 100):
    raise ValueError("sorry, you lose")

    This works because and/or can not start an expression, so it is
    obvious
    that they continue the expression from the previous line. If I run
    out
    of room to put a full and/or clause on one line, then I'll indent the
    subclause two levels.

    -- Greg
  • Pete Forman at Jun 9, 2010 at 4:27 pm
    HH <henrikho at gmail.com> writes:
    I have a question about best practices when it comes to line
    wrapping/ continuation and indentation, specifically in the case of
    an if statement.
    There are several good suggestions for formatting but no-one has
    mentioned rewriting the code. Use a boolean variable to hold the
    result of the condition and then the if statement is more readable.
    --
    Pete Forman -./\.-
    West Sussex, UK -./\.-
    http://petef.22web.net -./\.-
    petef4+usenet at gmail.com -./\.-
  • Jean-Michel Pichavant at Jun 9, 2010 at 4:53 pm

    Pete Forman wrote:
    HH <henrikho at gmail.com> writes:
    I have a question about best practices when it comes to line
    wrapping/ continuation and indentation, specifically in the case of
    an if statement.
    There are several good suggestions for formatting but no-one has
    mentioned rewriting the code. Use a boolean variable to hold the
    result of the condition and then the if statement is more readable.
    It has been suggested. ;)

    JM

Related Discussions

People

Translate

site design / logo © 2022 Grokbase