I tend to use this a lot, but it’s ugly:
a = (lambda x: x if x else y)(get_something())
So I wrote this function:
def either(val, alt):
if val:
return val
else:
return alt
So you can do:
a = either(get_something(), y)
Is there a built-in function for this (similar to ISNULL in T-SQL)?
The
oroperator does what you want:In fact, it’s chainable, like
COALESCE(and unlikeISNULL). The following expression evaluates to the left-most argument that converts to True.