Code formatting question: conditional expression

Jean-Michel Pichavant jeanmichel at sequans.com
Tue Aug 18 10:32:02 EDT 2009


Diez B. Roggisch wrote:
> 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
>   
+1, I'm using such layout whenever possible.

JM



More information about the Python-list mailing list