Run time default arguments
Carl Banks
pavlovevidence at gmail.com
Sat Aug 27 07:35:21 EDT 2011
On Thursday, August 25, 2011 1:54:35 PM UTC-7, ti... at thsu.org wrote:
> On Aug 25, 10:35 am, Arnaud Delobelle <arn... at gmail.com> 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 '=='
> > HTH
>
> 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?
I don't; it seems kind of superfluous when "if arg is not None: arg = whatever" is just as easy to type and more straightforward to read.
I could see a function like coalesce being helpful if you have a list of several options to check, though. Also, SQL doesn't give you a lot of flexibility, so coalesce is a lot more needed there.
But for simple arguments in Python, I'd recommend sticking with "if arg is not None: arg = whatever"
Carl Banks
More information about the Python-list
mailing list