python - 正常参数与关键字参数

“关键字参数”与常规参数有何不同? 不能将所有参数都传递为name=value而不是使用位置语法?

9个解决方案

300 votes

有两个相关的概念,都称为“关键字参数”。

在调用方面,这是其他评论者提到的,您可以通过名称指定一些函数参数。 你必须在没有名字(位置参数)的所有参数之后提及它们,并且必须有任何根本没有提到的参数的默认值。

另一个概念是在函数定义方面:您可以定义一个按名称获取参数的函数 - 您甚至不必指定这些名称是什么。 这些是纯关键字参数,不能在位置上传递。 语法是

def my_function(arg1, arg2, **kwargs)

您传递给此函数的任何关键字参数都将放入名为kwargs的字典中。 您可以在运行时检查此字典的键,如下所示:

def my_function(**kwargs):
print str(kwargs)
my_function(a=12, b="abc")
{'a': 12, 'b': 'abc'}
Ian Clelland answered 2019-02-28T08:47:02Z
161 votes

最后一种语言特征是区别很重要。 考虑以下功能:

def foo(*positional, **keywords):
print "Positional:", positional
print "Keywords:", keywords
**keywords参数将存储传递给foo()的所有位置参数,不限制您可以提供的数量。
>>> foo('one', 'two', 'three')
Positional: ('one', 'two', 'three')
Keywords: {}
**keywords参数将存储任何关键字参数:
>>> foo(a='one', b='two', c='three')
Positional: ()
Keywords: {'a': 'one', 'c': 'three', 'b': 'two'}

当然,您可以同时使用两者:

>>> foo('one','two',c='three',d='four')
Positional: ('one', 'two')
Keywords: {'c': 'three', 'd': 'four'}

这些功能很少使用,但偶尔它们非常有用,知道哪些参数是位置或关键字很重要。

too much php answered 2019-02-28T08:48:00Z
91 votes

使用关键字参数与普通参数相同,除了顺序无关紧要。 例如,下面的两个函数调用是相同的:

def foo(bar, baz):
pass
foo(1, 2)
foo(baz=2, bar=1)
Eli Grey answered 2019-02-28T08:48:28Z
39 votes

位置参数

他们之前没有关键字。 订单很重要!

func(1,2,3, "foo")

关键字参数

他们在前面有关键字。 它们可以按任何顺序排列!

func(foo="bar", baz=5, hello=123)
func(baz=5, foo="bar", hello=123)

您还应该知道,如果您使用默认参数并忽略插入关键字,那么订单将很重要!

def func(foo=1, baz=2, hello=3): ...
func("bar", 5, 123)
Unknown answered 2019-02-28T08:49:22Z
23 votes

有两种方法可以将参数值分配给函数参数,两种方法都可以使用。

按位置。 位置参数没有关键字并且首先分配。

按关键字。 关键字参数具有关键字,并在位置参数后分配第二个。

请注意,您可以选择使用位置参数。

如果你不使用位置参数,那么 - 是的 - 你写的所有东西都是一个关键字参数。

当您调用函数时,您决定使用位置或关键字或混合。 如果需要,您可以选择执行所有关键字。 我们中的一些人没有做出这个选择并使用位置参数。

S.Lott answered 2019-02-28T08:50:25Z

21 votes

我很惊讶,似乎没有人指出可以传递一个满足形式参数的键控参数字典,就像这样。

>>> def func(a='a', b='b', c='c', **kwargs):
... print 'a:%s, b:%s, c:%s' % (a, b, c)
...
>>> func()
a:a, b:b, c:c
>>> func(**{'a' : 'z', 'b':'q', 'c':'v'})
a:z, b:q, c:v
>>>
sherman answered 2019-02-28T08:50:51Z
13 votes

使用Python 3,您可以同时拥有必需和非必需的关键字参数:

可选:(为'b'定义的默认值)

def func1(a, *, b=42):
...
func1(value_for_a) # b is optional and will default to 42

必需(没有为'b'定义的默认值):

def func2(a, *, b):
...
func2(value_for_a, b=21) # b is set to 21 by the function call
func2(value_for_a) # ERROR: missing 1 required keyword-only argument: 'b'`

这可以帮助你有很多相似的参数紧挨着彼此,特别是在相同类型的情况下,在这种情况下,我更喜欢使用命名参数,或者我创建一个自定义类,如果参数属于一起。

Christophe Roussy answered 2019-02-28T08:51:39Z

11 votes

我很惊讶没有人提到你可以使用*args和**kwargs(来自这个网站)混合位置和关键字参数来做这样的偷偷摸摸的事情:

def test_var_kwargs(farg, **kwargs):
print "formal arg:", farg
for key in kwargs:
print "another keyword arg: %s: %s" % (key, kwargs[key])

这允许您使用任意关键字参数,这些参数可能包含您不希望预先定义的键。

Gabriel Hurley answered 2019-02-28T08:52:13Z

0 votes

我正在寻找一个使用类型注释的默认kwargs示例:

def test_var_kwarg(a: str, b: str='B', c: str='', **kwargs) -> str:
return ' '.join([a, b, c, str(kwargs)])

例:

>>> print(test_var_kwarg('A', c='okay'))
A B okay {}
>>> d = {'f': 'F', 'g': 'G'}
>>> print(test_var_kwarg('a', c='c', b='b', **d))
a b c {'f': 'F', 'g': 'G'}
>>> print(test_var_kwarg('a', 'b', 'c'))
a b c {}
jmunsch answered 2019-02-28T08:52:41Z