[Python-Dev] conditional expressions - add parens?

Steve Holden steve at holdenweb.com
Mon Mar 6 22:34:51 CET 2006


Jim Jewett wrote:
>   Now that we have started to see conditional expressions ... I would 
> like to see them parenthesized.  The parens aren't as important as they 
> are with generator expressions, but ... they matter.
> 
>  From a recent checkin --
> 
>     level = 0 if "absolute_import" in self.futures else -1
> 
Personally I think the above code exemplifies exactly why Guido was 
correct in his reluctance to add conditional expressions.

> Mentally, I can't help parsing that as "level = 0" plus comments that 
> turn out to be code that triggers backracking.
> 
> When the expressions (particularly the true case) are longer, it gets 
> even worse.
> 
> I think that adding parentheses would help, by at least signalling that 
> the logic is longer than just the next (single) expression.
> 
>     level = (0 if "absolute_import" in self.futures else -1)
> 
Contrast with the bleeding obvious:

     level = 0
     if "absolute_import" in self.futures:
         level = -1

or even, if a certain obscurity is desirable:

     level = - ("absolute_import" in self.futures)

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd                 www.holdenweb.com
Love me, love my blog         holdenweb.blogspot.com



More information about the Python-Dev mailing list