Skip to content
Advertisement

Python equivalent for MySQL’s IFNULL

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?

Advertisement

Answer

Not really, since you can’t rebind arguments.

if foo is None:
  foo = 42

or

def ifnull(var, val):
  if var is None:
    return val
  return var

foo = ifnull(foo, 42)
Advertisement