Run time default arguments

T

ting

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

Basically, I want to define a set of common defaults at the module and/
or class level but let them be overridden via function arguments. The
simpler, naive approach does not work, because the argument gets set
at compile time, rather than at run time. That is:
def doSomething(debug = defaults['debug'])
ends up being compiled into:
def doSomething(debug = false)
which is not the behavior I want, as I want to evaluate the argument
at run time.

Any better suggestions?
 
A

Arnaud Delobelle

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
 
M

MRAB

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

Basically, I want to define a set of common defaults at the module and/
or class level but let them be overridden via function arguments. The
simpler, naive approach does not work, because the argument gets set
at compile time, rather than at run time. That is:
def doSomething(debug = defaults['debug'])
ends up being compiled into:
def doSomething(debug = false)
which is not the behavior I want, as I want to evaluate the argument
at run time.

Any better suggestions?
The recommended way is:

def doSomething (debug = None):
if debug is None:
debug = defaults['debug']

It's more lines, but clearer.
 
T

ting

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?

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
 
C

Chris Angelico

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

It won't work with a True/False flag, but if you can guarantee that
all real options will evaluate as True, you can use a simpler
notation:

def doSomething(option=None):
option=option or defaults['option']

You can't explicitly set option to False/0 with this, but it is a bit
cleaner (IMO - some may disagree).

ChrisA
 
C

Carl Banks

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
 
S

Stephen Hansen

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,

Well, elegance is in the eye of the beholder: and the above idiom is
generally considered elegant in Python, more or less. (The global nature
of 'defaults' being a question)
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

Er, I'd say that most people don't do that, no. I'd guess that most do
something more along the lines of "if debug is None: debug = default" as
Arnaud said. Its very common Pythonic code.

In fact, I'm not quite sure what you think you're getting out of that
coalesce function. "Return the first argument that is not None, or
return None"? That's a kind of odd thing to do, I think. In Python at
least.

Why not just:

debug = defaults.get("debug", None)

(Strictly speaking, providing None to get is not needed, but I always
feel odd leaving it off.)

That's generally how I spell it when I need to do run time defaults.

--

Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iQEcBAEBAgAGBQJOWTvnAAoJEKcbwptVWx/lRrAH/2xNhUlUTgzmoriSOik/MzZQ
KwhUEQRifHxW6QGcPJxstcDgUbOuIpIRmfsvWlz4t96a18gY3bxUUCy95z7WD1P4
GmhDEvTgih8W2DjtgSb/LwJv/YLaiSyKQtiP1Uy/Ydb9HWHts4YkVEbhCIO76b58
cf1mPvepk5RxaT7GQvfXTIqsKkgPfdDG9GcYxiDoA9ukjxxVBTEYIxcYAkd+73Og
meWIz3ARGGirfqGVvk9hOFmGUGIAFQZzyD08QS0g4rfTpoFHoYBQttVn0xtAgq/N
BpVehXVVXrVZO2wmJKcbDpmbneTan2J4kHbt/ZA3Kj4OczElqX2UgAg2nQ09f7s=
=4I1s
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top