Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

0 views
Skip to first unread message

Noah

unread,
Dec 22, 2005, 5:36:05 PM12/22/05
to
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[i]
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

Fredrik Lundh

unread,
Dec 22, 2005, 6:04:07 PM12/22/05
to pytho...@python.org
"Noah" <no...@noah.org> wrote:

> 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.

>>> import inspect
>>> help(inspect.getargspec)
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.

>>> def foo(f1, f2, f3, f4="F4", f5="F5"):
... 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'}

>>> class fu:
... 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>

Noah

unread,
Dec 22, 2005, 6:12:18 PM12/22/05
to

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

Yours,
Noah

0 new messages