Is there shorthand for returning a default value if None in Python? [duplicate]
You could use the
return x or "default"
Note that this also returns
x is any falsy value, including an empty list, 0, empty string, or even
You can use a conditional expression:
x if x is not None else some_value
In : x = NoneIn : print x if x is not None else "foo"fooIn : x = "bar"In : print x if x is not None else "foo"bar