Can You Determine From A Function, Args, And Kwargs, How Variables Will Be Assigned?
I have some boiler plate logic that I want to wrap several functions that have the same optional keyword.  Right now it looks like the code below.  However, this only handles the c
Solution 1:
Yes, you want to use inspect.signature and work with the Signature object, which will allow you to .bind *args and **kwargs:
In [1]: import inspect
In [2]: args = 'a', 'b', 'c'
In [3]: kwargs = {}
In [4]: defg(x,y,opt_key = None):
   ...:   return something
   ...:
   ...: defh(z,opt_key = None):
   ...:   return something_else
   ...:
In [5]: g_signature = inspect.signature(g)
In [6]: g_signature.bind(*args, **kwargs)
Out[6]: <BoundArguments (x='a', y='b', opt_key='c')>
Note, this BoundArguments object acts as a mapping:
In [7]: bound_arguments = g_signature.bind(*args, **kwargs)
In [8]: bound_arguments.arguments['opt_key']
Out[8]: 'c'
Post a Comment for "Can You Determine From A Function, Args, And Kwargs, How Variables Will Be Assigned?"