Code formatting question: conditional expression
Diez B. Roggisch
deets at nospam.web.de
Tue Aug 18 10:10:01 EDT 2009
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? How about this:
>
> excessblk = (Block(total - P.BASE, srccol, carry_button_suppress=True)
> if total > P.BASE else
> None)
>
> The above format separates the values from the if-then-else machinery.
> Too many lines? Would it be better to line up "if" and "else"
> vertically? ...
>
> excessblk = (Block(total - P.BASE, srccol, carry_button_suppress=True)
> if total > P.BASE
> else None)
My choice would be
excessblk = None
if total > P.BASE:
excessblk = ...
You don't lose any vertical space, and it's much more readable IMHO.
Diez
More information about the Python-list
mailing list