Code formatting question: conditional expression

Jan Kaliszewski zuo at chopin.edu.pl
Tue Aug 18 21:48:58 CEST 2009


18-08-2009 Steven D'Aprano <steve at remove-this-cybersource.com.au> wrote:

> On Tue, 18 Aug 2009 10:04:36 -0400, John Posner wrote:
>
[snip]
>> How about this:
>>
>>   excessblk = (Block(total - P.BASE, srccol, carry_button_suppress=True)
>>                if total > P.BASE else
>>                None)
>
> If you insist on using the conditional expression, my preference would  
> be:
>
>   excessblk = (
>     Block(total - P.BASE, srccol, carry_button_suppress=True)
>     if total > P.BASE else None
>     )

Generally I'd prefer:

  excessblk = (Block(........) if total > P.BASE
               else None)

But it this case first line would be too long, then I'd try using:

  excessblk = (Block(total - P.BASE, srccol,
                     carry_button_suppress=True) if total > P.BASE
               else None)

or

  excessblk = (Block(total - P.BASE, srccol, carry_button_suppress=True)
               if total > P.BASE else None)

I'd use conditional expression only (rather) in situation when the first
expression-part was 'common' and the other (after else) was 'rare'.

Cheers,
*j

-- 
Jan Kaliszewski (zuo) <zuo at chopin.edu.pl>



More information about the Python-list mailing list