There is a very common sentence that I use over and over in Python that feels "too long", and I am hoping there is a more Pythonic way to write it:
a = b if a is None else a
Is there a better way to write this?
The typical shortcut for this is
a = a or b
Though, this will only test if a
evaluates to False
, so 0
, False
, None
, ''
, etc. will all cause a
to be set to b
You could do...
if a is None:
a = b
It seems a bit cleaner to me and you don't have to assign a variable to itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With