Run time default arguments

ting at ting at
Thu Aug 25 16:54:35 EDT 2011

On Aug 25, 10:35 am, Arnaud Delobelle <arno... at> wrote:
> You're close to the usual idiom:
> def doSomething(debug=None):
>     if debug is None:
>         debug = defaults['debug']
>     ...
> Note the use of 'is' rather than '=='

Hmm, from what you are saying, it seems like there's no elegant way to
handle run time defaults for function arguments, meaning that I should
probably write a sql-esc coalesce function to keep my code cleaner. I
take it that most people who run into this situation do this?

def coalesce(*args):
  for a in args:
    if a is not None:
      return a
  return None

def doSomething(debug=None):
  debug = coalesce(debug,defaults['debug'])
  # blah blah blah
// T.Hsu

More information about the Python-list mailing list