From: Harald Luessen on
On Thu, 27 May 2010 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.
>
> if (width == 0 and
> height == 0 and
> color == 'red' and
> emphasis == 'strong' or
> highlight > 100):
> raise ValueError("sorry, you lose")

My solution would probably look like this:

if ( width == 0
and height == 0
and color == 'red'
and emphasis == 'strong'
or highlight > 100
):
raise ValueError("sorry, you lose")

Harald

From: Alain Ketterlin on
HH <henrikho(a)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.
From: Jean-Michel Pichavant on
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
From: HH on
On May 27, 11:37 am, Alain Ketterlin <al...(a)dpt-info.u-strasbg.fr>
wrote:
> HH <henri...(a)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")

Thanks for all suggestions! I like this solution a lot.

I agree with your statement about mixed operators and explicit
parentheses -- expecially since the eye/brain parser often does it
wrong, and I believe you demonstrated that above...

In [29]: print(False and (False or True))
False
In [30]: print(False and False or True)
True

> 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...

Yes, I much prefer the parentheses -- partly because PEP-8 suggests it
but mostly because the escapes get lost too easily.


HH
From: Jonathan Hartley on
On May 27, 1:57 pm, Jean-Michel Pichavant <jeanmic...(a)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.
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5
Prev: type error raise
Next: Minor annoyances with properties