Is there shorthand for returning a default value if None in Python?
return "default" if x is None else x
try the above.
You could use the or
operator:
return x or "default"
Note that this also returns "default"
if x
is any falsy value, including an empty list, 0, empty string, or even datetime.time(0)
(midnight).