ALL function arguments in a common dictionary

Fábio Santos fabiosantosart at gmail.com
Wed Apr 24 09:48:56 EDT 2013


A quick hack:

>>> from inspect import getargspec
>>>
>>> def func(a, b, c=3, **kwargs):
...     out_dict = {}
...     args, _, keywords, _ = getargspec(func)
...     for argname in args:
...         out_dict[argname] = locals()[argname]
...     if keywords:
...         out_dict.update(locals()[keywords])
...     return out_dict
...
>>> func(1,2, gah=123)
{'a': 1, 'c': 3, 'b': 2, 'gah': 123}
>>>

On Wed, Apr 24, 2013 at 2:36 PM, Wolfgang Maier
<wolfgang.maier at biologie.uni-freiburg.de> wrote:
> Hi everybody,
> what is the recommended way of stuffing *all* function arguments (not just
> the ones passed by **kwargs) into a common dictionary?
>
> The following sort of works when used as the first block in a function:
> try:
>     kwargs.update(locals())
> except NameError:
>     kwargs = locals().copy()
>
> except that it's nesting pre-existing kwargs.
>
> Thanks for your help,
> Wolfgang
>
> --
> http://mail.python.org/mailman/listinfo/python-list



-- 
Fábio Santos



More information about the Python-list mailing list