将args和kwarg规范化为kwarg-规范形式

时间:2019-04-29 09:55:29

标签: python variadic-functions

我曾问过类似的问题here,以posarg规范形式。

任何可能的参数都应转换为关键字形式,并根据is运算符,id或类似的结果省略默认值。

例如:

def myfunc(a, b=None, **kwargs):
    pass

def canonicalize(func, *args, **kwargs):
    something = inspect.signature(func)
    # Do something with args/kwargs here
    return new_args, new_kwargs

示例输出:

>>> canonicalize(myfunc, 1, 2, g=3)
(1,), {'b': 2, 'g': 3}
>>> canonicalize(myfunc, 1)
(1,), {}
>>> canonicalize(myfunc, 1, b=2)
(1,), {'b': 2}
>>> canonicalize(myfunc, 1, g=3, b=None)
(1,), {'g': 3}

1 个答案:

答案 0 :(得分:1)

您可以使用以下函数-您基本上将具有默认值的参数移动到** kwargs中的任何值中(如果实际上并未分配给它自己的默认值):

import inspect

def canonicalize(f, *args, **kwargs):
    sig = inspect.signature(f)
    bargs = sig.bind(*args, **kwargs)
    # Pop out the named kwargs variable defaulting to {}
    ret_kwargs = bargs.arguments.pop(inspect.getfullargspec(f).varkw, {})
    # For all possible signature values
    for k, v in sig.parameters.items():
        # If the name exists in the bound arguments and has a default value
        if k in bargs.arguments and v.default is not inspect.Parameter.empty:
            # Remove from the bound arguments dict
            val = bargs.arguments.pop(k)
            # If the value isn't the same as the default value add it to ret_kwargs
            if val is not v.default:
                ret_kwargs[k] = val

    # bargs.args here will be made up of what's left in bargs.arguments
    return bargs.args, ret_kwargs