How do I get a dictionary of argument names with their default values?

N

Noah

Is there a simple way to get a dictionary of
argument names and their default values for
a method or function? I came up with one solution, but
I feel like Python must have a simpler way.

Python provide a way to get a sequence of
argument names and a different way to get a
sequence of default argument values. Since
default values have to come at the end of an
argument list you can match up the values in the
default values sequence to the values in the
argument list sequence.

The following seems to work on both functions and methods::

def func_arg_defaults_dict (func):
default_dict = {}
if func.func_defaults is not None:
da_start = len(func.func_code.co_names) -
len(func.func_defaults)
for i in range(len(func.func_defaults)):
arg_name = func.func_code.co_names[da_start+i]
arg_value = func.func_defaults
default_dict [arg_name] = arg_value
return default_dict

def foo (f1, f2, f3, f4='F4', a5='F5'):
print f1, f2, f3, f4, f5

class fu:
def foo (self, m1, m2, m3, m4='M4', m5='M5'):
print m1, m2, m3, m4, m5

f = fu()

# test function
print func_arg_defaults_dict (foo)
# test method
print func_arg_defaults_dict (f.foo)

Running the script prints this::

{'f4': 'F4', 'f5': 'F5'}
{'m5': 'M5', 'm4': 'M4'}

Is func_arg_defaults_dict() reasonable? It seems a bit convoluted.
I hope there is a more obvious way.

Yours,
Noah
 
F

Fredrik Lundh

Noah said:
Is there a simple way to get a dictionary of
argument names and their default values for
a method or function? I came up with one solution, but
I feel like Python must have a simpler way.
Help on function getargspec in module inspect:

getargspec(func)
Get the names and default values of a function's arguments.

A tuple of four things is returned: (args, varargs, varkw, defaults).
'args' is a list of the argument names (it may contain nested lists).
'varargs' and 'varkw' are the names of the * and ** arguments or None.
'defaults' is an n-tuple of the default values of the last n arguments.
.... pass
....
inspect.getargspec(foo) (['f1', 'f2', 'f3', 'f4', 'f5'], None, None, ('F4', 'F5'))
args, varargs, varkw, defaults = inspect.getargspec(foo)
dict(zip(args[-len(defaults):], defaults))
{'f4': 'F4', 'f5': 'F5'}
.... def foo (self, m1, m2, m3, m4='M4', m5='M5'):
.... pass
....
f = fu()
inspect.getargspec(f.foo) (['self', 'm1', 'm2', 'm3', 'm4', 'm5'], None, None, ('M4', 'M5'))
args, varargs, varkw, defaults = inspect.getargspec(f.foo)
dict(zip(args[-len(defaults):], defaults))
{'m5': 'M5', 'm4': 'M4'}

</F>
 
N

Noah

Thanks! Much more handy than what I was trying to do
and it looks like I might even learn something new :)

Yours,
Noah
 

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

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top