Many SO discussions and search queries have not been able to definitively answer the question of the best / safest / most Pythonic way of providing a default value if the Python function receives Nonein a parameter. This is especially true for a parameter datetimeif it matters, but ideally we standardize our approach for all types of parameters.
Here are two approaches that both fit as the βrightβ way to do this:
myval if myval else defaultvalmyval or defaultval
Are they functionally equivalent or are there subtle differences between them? I very much prefer the brevity and clarity of the second option, but the proponents of the first say that it is not always safe. Any guidance from someone who has more python experience than me (like almost anyone) would be greatly appreciated.
source
share