if, continuation and indentation

Jean-Michel Pichavant jeanmichel at sequans.com
Thu May 27 11:46:21 EDT 2010


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



More information about the Python-list mailing list