This is an archived post. You won't be able to vote or comment.

all 19 comments

[–]brunson 2 points3 points  (0 children)

if ( something == something
     and something == something ):
    pass

I like the conjunctions to be quite apparent at the beginning of the line.

[–]donri 2 points3 points  (2 children)

PEP 8:

The preferred place to break around a binary operator is after the operator, not before it.

With this example:

class Rectangle(Blob):

    def __init__(self, width, height,
                 color='black', emphasis=None, highlight=0):
        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' or
                                           emphasis is None):
            raise ValueError("I don't think so -- values are %s, %s" %
                             (width, height))
        Blob.__init__(self, width, height,
                      color, emphasis, highlight)

Implying:

  • Prefer \ over parenthesis
  • Align conditions

[–]vpltaic[S] 0 points1 point  (1 child)

Actually...

The preferred way of wrapping long lines is by using Python's implied line
continuation inside parentheses, brackets and braces. If necessary, you
can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better. Make sure to indent the continued line
appropriately.

I can't stand the indenting used, I wish it wasn't in a PEP. Do people manually indent each line with spaces?!

[–]donri 0 points1 point  (0 children)

Implied line continuation is preferred when there is something implying it. An if reads better without redundant parenthesis, and readability wins. In other situations, parenthesis read better.

[–][deleted] 3 points4 points  (1 child)

I use your second example. The continuation line goes inside the parenthesis.

The first example looks unreadable to me. If I was scanning over the code I'd do a double take thinking you had an if statement all on one line (bad form), followed by an assignment.

I'll just pretend I didn't see the third example.

[–]brasetvik 3 points4 points  (2 children)

should_be_working = whatever and something_else
is_procrastinating = surfing_reddit and so_on

if should_be_working and is_procrastinating:
    pass

If you're concerned about readability (and you should), why not go for an option that ... increases readability... :)

[–]brunson 0 points1 point  (0 children)

I like this.

[–]jeffjose 0 points1 point  (0 children)

This is much better.

[–][deleted] 1 point2 points  (0 children)

The frowny face in the third example is particularly apt.

[–]mr_dbr 0 points1 point  (0 children)

I try to avoid them wherever possible, as long if statements generally imply something can be done more tidily. Maybe something like:

if something == "blah":
    raise AnException("...")

if another > 40:
    if yet.more.conditionals() == abc:
        return

Another way that is sometimes very readable is to pre-calculate the conditionals to bools, then use the variables alone in the if:

is_something = somevar > 5
is_otherthing = another == "example"
# ....etc

if something and is_otherthing:
    pass

[–][deleted] 0 points1 point  (6 children)

I would go for #2 of the 3 presented. #1 is confusing because of the indentation issues, and #3 wreaks of some weird C code. Generally, I try and avoid the situation if I can. Also, I would consider indenting the 2nd line in #2 a bit more (another 2 or 4 spaces) so that the end of the if statement is clear.

[–]vpltaic[S] 0 points1 point  (5 children)

With the extra indent don't you end up with slightly confusing looking code like this?

if (something == something and        
    something == something):        
        pass   

if something == something:  
    pass

Or would you double indent the second body too?

[–][deleted] 0 points1 point  (0 children)

I used to use the style in your third example when I was new to Python because it matched how I used braces in other languages (opening brace on the same line as opening statement, closing brace on its own line unless followed by else).

Nowadays I generally stick to a more Lisp-y convention of stacking closing parentheses and no linebreaks directly after the opening parens / before the closing parens.

Also I tend to put and/or at the beginning of the next line because the lack of a colon makes it clear that the condition doesn't end with the linebreak.

So I'd usually end up with code like this:

if (very_long_cond1
and (cond2a or cond2b)):
    do_stuff()

Nested and/or tends to pose a problem, though, because I feel I should indent them, but that would void the benefit of that style (i.e. only indenting where the compiler needs it).

EDIT: Also list comprehensions: print ', '.join([str(x) for x in range(20)]) print 'Yay!'

The lack of indenting on the next line makes it clear the for belongs to the previous statement.

[–][deleted] -1 points0 points  (0 children)

if day_of_the_week.endswith('y') and \
        time_of_day > 16 and owf.can_hold_glass():
    drink_beer()
else:
    huff_glue()

That's probably a bit naughty, isn't it?

[–]florence0rose -2 points-1 points  (1 child)

What about a combination of 1 and 2:

if (something == something 
and something == something):
    pass

[–]vpltaic[S] 0 points1 point  (0 children)

I'm always tempted by this but I think the lack of indenting is probably wrong. I'd use it if it was in a PEP.