Is there a function in Python that checks if the returned value is None and if it is, allows you to set it to another value like the IFNULL
function in MySQL?
If you want to convert all "falsy" values (i.e. None
, 0
, ""
, []
, False
, etc.) to a specific value and let everything else through untouched, you can use or
. For example:
print (x or default_value)
will print the value of x
if it's truthy, and the value of default_value
if x
is falsy.
I mention this because IFNULL
is often used this way to clean up nulls in boolean and numerical columns in a database and so might be what you or others were after. Obviously, if you want to treat None
differently to 0
, False
, etc. this won't work.
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