One liners

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Dec 7 11:25:07 EST 2013


On Sat, 07 Dec 2013 16:13:09 +0000, Rotwang wrote:

> 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?

Only if you need to support Python 2.4, which doesn't have the ternary if 
operator :-)



-- 
Steven



More information about the Python-list mailing list