Run time default arguments

Arnaud Delobelle arnodel at gmail.com
Thu Aug 25 10:35:05 EDT 2011


On Aug 25, 3:30 pm, t... at thsu.org wrote:
> What is the most common way to handle default function arguments that
> are set at run time, rather than at compile time? The snippet below is
> the current technique that I've been using, but it seems inelegant.
>
> defaults = { 'debug' : false }
> def doSomething (debug = None):
>     debug = debug if debug != None else defaults['debug']
>     # blah blah blah

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 '=='
HTH

--
Arnaud



More information about the Python-list mailing list