当涉及到默认值时,在Python中使用**kwargs的正确方法是什么?

Kwargs返回一个字典,但是设置默认值的最佳方法是什么?我应该把它作为字典来访问吗?使用get函数?

class ExampleClass:
    def __init__(self, **kwargs):
        self.val = kwargs['val']
        self.val2 = kwargs.get('val2')

一个简单的问题,但我找不到好的资料。在我所见过的代码中,人们用不同的方式来实现它,很难知道该用什么。


当前回答

你可以这样做

class ExampleClass:
    def __init__(self, **kwargs):
        arguments = {'val':1, 'val2':2}
        arguments.update(kwargs)
        self.val = arguments['val']
        self.val2 = arguments['val2']

其他回答

跟进@srhegde使用setattr的建议:

class ExampleClass(object):
    __acceptable_keys_list = ['foo', 'bar']

    def __init__(self, **kwargs):
        [self.__setattr__(key, kwargs.get(key)) for key in self.__acceptable_keys_list]

当期望类拥有可接受列表中的所有项时,此变体非常有用。

我认为在Python中使用**kwargs的正确方法是使用字典方法setdefault,如下所示:

class ExampleClass:
    def __init__(self, **kwargs):
        kwargs.setdefault('val', value1)
        kwargs.setdefault('val2', value2)

这样,如果用户在关键字args中传递'val'或'val2',它们将被使用;否则,将使用已设置的默认值。

如果你想把它和*args结合起来,你必须在定义的末尾保留*args和**kwargs。

So:

def method(foo, bar=None, *args, **kwargs):
    do_something_with(foo, bar)
    some_other_function(*args, **kwargs)

对于不在字典中的键,可以传递一个默认值给get():

self.val2 = kwargs.get('val2',"default value")

但是,如果您计划使用具有特定默认值的特定参数,为什么不首先使用命名参数呢?

def __init__(self, val2="default value", **kwargs):

虽然大多数答案都是这样说的,例如,

def f(**kwargs):
    foo = kwargs.pop('foo')
    bar = kwargs.pop('bar')
    ...etc...

是"the same as"

def f(foo=None, bar=None, **kwargs):
    ...etc...

this is not true. In the latter case, f can be called as f(23, 42), while the former case accepts named arguments only -- no positional calls. Often you want to allow the caller maximum flexibility and therefore the second form, as most answers assert, is preferable: but that is not always the case. When you accept many optional parameters of which typically only a few are passed, it may be an excellent idea (avoiding accidents and unreadable code at your call sites!) to force the use of named arguments -- threading.Thread is an example. The first form is how you implement that in Python 2.

这个习语非常重要,以至于在Python 3中,它现在有了特殊的支持语法:def签名中单个*后面的每个参数都是仅关键字参数,也就是说,不能作为位置参数传递,而只能作为命名参数传递。所以在Python 3中,你可以将上面的代码编写为:

def f(*, foo=None, bar=None, **kwargs):
    ...etc...

实际上,在Python 3中,你甚至可以有非可选的关键字参数(没有默认值的参数)。

然而,Python 2仍然有很长的生产寿命,所以最好不要忘记让你在Python 2中实现重要的设计思想的技术和习语,这些思想在Python 3中直接得到语言的支持!