Code formatting question: conditional expression

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Tue Aug 18 10:54:36 EDT 2009


On Tue, 18 Aug 2009 10:04:36 -0400, John Posner wrote:

> While refactoring some code, I ran across an opportunity to use a
> conditional expression. Original:
> 
>   if total > P.BASE:
>       excessblk = Block(total - P.BASE, srccol,
>       carry_button_suppress=True)
>   else:
>       excessblk = None
> 
> Is there any consensus on how to format a conditional expression that is
> too long for one line?

Er, that defeats the purpose of using a conditional expression. If it's 
too long for one line, leave it as an if...else statement.

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


-- 
Steven



More information about the Python-list mailing list