python检查函数中需要kwargs

时间:2015-07-03 07:03:33

标签: python

我只想检查必须将所需的kwargs发送到函数:

def myfunca()
    kwargs = {'a'=1, 'b'=2}
    anyfunc(**kwargs)
    return

def anyfunc(**kwargs):

    check_kwargs(**kwargs)    

    print kwargs['a']
    print kwargs['c']
    total = kwargs['a'] + kwargs['c']
    return 


def check_kwargs(**kwargs):
    check if the kwargs required anyfunc is provided from myfunc() 
    if there is not required kwargs for anyfunc then raise appropriate errors 

需要建议

1 个答案:

答案 0 :(得分:0)

使用assert。例如,

def check_kwargs(kwargdict, requiredargs):
    for r in requiredargs:
        assert r in kwargdict.keys()

requiredargs将是一个包含需要存在的参数名称的iterable。您也应该从**的电话中移除check_kwargs,因为这没有任何意义。