Canonicalise args和kwargs to kwarg-canonical form

问题描述 投票:0回答:1

我问了类似的问题here的posarg-canonical形式。

任何可能的args都应转换为keyword-form,默认值基于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}
python variadic-functions
1个回答
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

推荐问答