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.<div>
<br></div><div>is_correct_style = width == 0 and height == 0 and color == 'red'</div><div><div>if (is_correct_style and emphasis == 'strong') or highlight > 100:</div></div><div><br></div><div><br><div class="gmail_quote">
On Sat, May 29, 2010 at 8:59 PM, Mark Lawrence <span dir="ltr"><<a href="mailto:breamoreboy@yahoo.co.uk">breamoreboy@yahoo.co.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">On 30/05/2010 01:23, john wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On May 28, 10:37 am, "Colin J. Williams"<<a href="mailto:cjwilliam...@gmail.com" target="_blank">cjwilliam...@gmail.com</a>><br>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On 28-May-10 05:54 AM, Jonathan Hartley wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On May 27, 1:57 pm, Jean-Michel Pichavant<<a href="mailto:jeanmic...@sequans.com" target="_blank">jeanmic...@sequans.com</a>><br>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
HH wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I have a question about best practices when it comes to line wrapping/<br>
continuation and indentation, specifically in the case of an if<br>
statement.<br>
</blockquote></blockquote></blockquote>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
When I write an if statement with many conditions, I prefer to use a<br>
parenthesis around the whole block and get the implicit continuation,<br>
rather than ending each line with an escape character. Thus, using<br>
the example from the style guide (<a href="http://www.python.org/dev/peps/" target="_blank">http://www.python.org/dev/peps/</a><br>
pep-0008/) I would write:<br>
</blockquote></blockquote></blockquote>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
if (width == 0 and<br>
height == 0 and<br>
color == 'red' and<br>
emphasis == 'strong' or<br>
highlight> 100):<br>
raise ValueError("sorry, you lose")<br>
</blockquote></blockquote></blockquote>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The problem should be obvious -- it's not easy to see where the<br>
conditional ends and the statement begins since they have the same<br>
indentation. Part of the problem, I suppose, is that Emacs indents<br>
'height' and the other lines in the conditional to 4 spaces (because<br>
of the parenthesis). How do people deal with this situation?<br>
</blockquote></blockquote></blockquote>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Thanks,<br>
Henrik<br>
</blockquote></blockquote></blockquote>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
One possible solution<br>
</blockquote></blockquote>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
if (<br>
width == 0 and<br>
height == 0 and<br>
color == 'red' and<br>
emphasis == 'strong' or<br>
highlight> 100<br>
):<br>
raise ValueError("sorry, you lose")<br>
</blockquote></blockquote>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
JM<br>
</blockquote></blockquote>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I've always liked this, or even:<br>
</blockquote>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
if (<br>
width == 0 and<br>
height == 0 and<br>
color == 'red' and<br>
emphasis == 'strong' or<br>
highlight> 100<br>
):<br>
raise ValueError("sorry, you lose")<br>
</blockquote>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
but my co-workers have uniformly gone bananas whenever I try it.<br>
</blockquote>
<br>
I liked:<br>
<br>
On 27-May-10 08:48 AM, Xavier Ho wrote:<br>
> On 27 May 2010 22:22, HH<<a href="mailto:henri...@gmail.com" target="_blank">henri...@gmail.com</a>> <mailto:<a href="mailto:henri...@gmail.com" target="_blank">henri...@gmail.com</a>>> wrote:<br>
<br>
><br>
> if (width == 0 and<br>
> height == 0 and<br>
> color == 'red' and<br>
> emphasis == 'strong' or<br>
> highlight> 100):<br>
> raise ValueError("sorry, you lose")<br>
><br>
><br>
> I've gotta say - I've bumped into this problem before, and I'm sure many<br>
> other have - this is a valid question. It just hasn't bothered me enough<br>
> to ask...<br>
><br>
> Correct me if I'm wrong, but I think the following is equivalent, and<br>
> looks better. Although this won't fix all ugly cases in that problem..<br>
><br>
> if (width, height, color, emphasis) == (0, 0, 'red', 'strong') or<br>
> highlight> 100:<br>
> raise ValueError("sorry, you lose")<br>
><br>
> Cheers,<br>
> Xav<br>
<br>
but nobody commented.<br>
<br>
Colin W.<br>
</blockquote>
<br>
Colin:<br>
Sure, you can do it that way. IMO, though, the OP was wrong, and so<br>
is the PEP. Source code is meant to communicate. So it must transmit<br>
the correct information to the computer; it also must inform your<br>
coworkers. That means that you have a responsibility to care what<br>
they think, though you privately have your opinions. Another reason<br>
the PEP is faulty in this circumstance is that a misplaced backslash,<br>
or a missing one, is easily found and fixed. A misplaced parentheses,<br>
or just one of a pair, will transform your source code into something<br>
which may compile and then give faulty results: a disaster.<br>
So keep it simple, and make it legible.<br>
Yours,<br>
John<br>
</blockquote>
<br></div></div>
IMHO complete garbage, if your editor doesn't show misplaced or missing parenthesis by highlighting you're using the wrong editor :)<br>
<br>
Regards.<br><font color="#888888">
<br>
Mark Lawrence.</font><div><div></div><div class="h5"><br>
<br>
<br>
-- <br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br></div>