If I have a Python function defined as f(a, b, c)
, is there a straightforward way to get a dictionary mapping the formal names to the values passed in? That is, from inside f
, I'd like to be able to get a dictionary {'a': 1, 'b': 2, 'c': 3}
for the call f(1, 2, 3)
.
I'd like to do this so I can directly use the arguments and values in a string substitution, e.g. "%(a)s %(b)s %(c)s" % d
. I could just do something like d = dict(a=a, b=b, c=c)
, but I'd rather avoid the repetition if possible.
I know this is quite easy to do by defining f
as f(**kwds)
, but that also makes it less obvious what arguments the function expects. It looks like there'd probably be some way to do this via the inspect
module, but it'd probably be too complex to be worthwhile.
I suspect there's no good answer to this question as posed, but I'd be happy to be proven wrong. Alternative approaches for accomplishing what I described above would be welcome too.