469,356 Members | 2,003 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

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

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

Dec 22 '05 #1
2 1642
"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>

Dec 22 '05 #2

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

Yours,
Noah

Dec 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by brianobush | last post: by
125 posts views Thread by Raymond Hettinger | last post: by
4 posts views Thread by Noah | last post: by
2 posts views Thread by Matt | last post: by
24 posts views Thread by kdotsky | last post: by
70 posts views Thread by jojoba | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.