<div dir="ltr">I was happy to see the new `getcallargs` function in the `inspect` module. But there's one thing I want to do that's related to it, and maybe this was implemented somewhere or someone can give me some pointers about it.<div>
<br></div><div>I want to have a function that takes the results of `getargspec` and `getcallargs` for a function and a set of arguments, and outputs the "canonical argument call" that would generate these results.</div>
<div><br></div><div>For example, let's say I have a function `f`:</div><div><br></div><div> def f(a, b, c=7, **kwargs):</div><div> pass<br clear="all"><div><br></div><div>This is its argument spec:</div><div>
<br></div><div> >>> inspect.getargspec(f)</div><div> ArgSpec(args=['a', 'b', 'c'], varargs=None, keywords='kwargs', defaults=(7,))</div><div><br></div><div>Now, different argument combinations can generate the same results `getcallargs` result</div>
<div><br></div><div> >>> inspect.getcallargs(f, 1, 2, 7, meow='grrr') # Like calling f(1, 2, 7, meow='grrr')</div><div> {'a': 1, 'c': 7, 'b': 2, 'kwargs': {'meow': 'grrr'}}</div>
<div> </div><div> >>> inspect.getcallargs(f, 1, 2, meow='grrr') # Like calling f(1, 2, meow='grrr')</div><div> {'a': 1, 'c': 7, 'b': 2, 'kwargs': {'meow': 'grrr'}}</div>
<div> </div><div> >>> inspect.getcallargs(f, 1, b=2, c=7, meow='grrr') # Like calling f(1, b=2, c=7, meow='grrr')</div><div> {'a': 1, 'c': 7, 'b': 2, 'kwargs': {'meow': 'grrr'}}</div>
</div><div><br></div><div>What I would like to have is one canonical argument combination. I assume that the best way to define the canonical form is "The smallest number of arguments, and specifying as few keyword arguments as possible." According to that definition, the canonical argument call in the above example would be the second one, `f(1, 2, meow='grrr')`.</div>
<div><br></div><div>So I want a function that for a given function and argument call, returns the canonical argument call, which generates the same `getcallargs` result as the given argument call.</div><div><br></div><div>
The reason I want this is to make it easier to organize a set of argument calls. (I'm doing an application in which the user maintains a set of argument profiles that he can choose from, and having to deal with only the canonical form will prevent some annoyances.)</div>
<div><br></div><div>Did anyone implement something like this? Does anyone have ideas? And is this something that would be of interest to the standard library?</div><div><br></div><div><br></div><div>Best Wishes,</div><div>
Ram Rachum.</div></div>