if, continuation and indentation

Alain Ketterlin alain at dpt-info.u-strasbg.fr
Thu May 27 11:37:30 EDT 2010


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.



More information about the Python-list mailing list