One liners

Jussi Piitulainen jpiitula at ling.helsinki.fi
Sat Dec 7 12:49:57 EST 2013


Rotwang writes:

> On 07/12/2013 12:41, Jussi Piitulainen wrote:
> > [...]
> >
> >    if tracks is None:
> >       tracks = []
> 
> Sorry to go off on a tangent, but in my code I often have stuff like
> this at the start of functions:
> 
>      tracks = something if tracks is None else tracks
> 
> or, in the case where I don't intend for the function to be passed
> non-default Falsey values:
> 
>      tracks = tracks or something
> 
> Is there any reason why the two-line version that avoids the ternary
> operator should be preferred to the above?

My motivation is that the "one-armed if" highlights the condition,
together with the fact that nothing is changed unless the condition
holds. That said, I'm also fine with the forms you use.

On another tangent, I wish people called the conditional expression
the conditional expression. The number of its slots is about the least
salient property of the thing. :)



More information about the Python-list mailing list