Most compact "X if X else Y" idiom

Terry Reedy tjreedy at udel.edu
Sun Oct 12 07:26:09 CEST 2008


jbperez808 at yahoo.com wrote:
> I find myself having to do the following:
> 
>   x = (some complex expression)
>   y = x if x else "blah"
> 
> and I was wondering if there is any built-in idiom that
> can remove the need to put (some complex expression)
> in the temporary variable x.

A common idiom for this particular case where the if-expression is also 
the conditional or the basic of the conditional expression is

y = <some complex expression>
if not y: y = "blah"




More information about the Python-list mailing list