if, continuation and indentation
Colin J. Williams
cjwilliams43 at gmail.com
Fri May 28 13:37:14 EDT 2010
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 <henrikho at gmail.com
> <mailto: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.
More information about the Python-list
mailing list