0

I am running a bunch of experiments and I am constantly finding myself rewriting code in order to document the parameters that are being passed into a function. The main idea is to get a dictionary with the names of the variables that the function got and the values that it got (including default values). Here are some examples of what I am after

def foo(a, b, c=3, d=4):
    pass

foo(1,2)
  {'a': 1, 'b':2, 'c':3, 'd':4}

foo(1,2,5,6)
  {'a': 1, 'b':2, 'c':5, 'd':6}

foo(1,2,d=10)
  {'a': 1, 'b':2, 'c':3, 'd':10}

I have looked into using kwargs and the inspect module but I am not able to get the specified behavior.

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

1

I think you need

def foo(a, b, c=3, d=4):
  return dict(locals())
Nannan AV
  • 419
  • 7
  • 22