Python:按名称和 kwargs 传递参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15074821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: Passing parameters by name along with kwargs
提问by CodeArtist
In python we can do this:
在python中,我们可以这样做:
def myFun1(one = '1', two = '2'):
...
Then we can call the function and pass the arguments by their name:
然后我们可以调用该函数并按名称传递参数:
myFun1(two = 'two', one = 'one')
Also, we can do this:
此外,我们可以这样做:
def myFun2(**kwargs):
print kwargs.get('one', 'nothing here')
myFun2(one='one')
So I was wondering if it is possible to combine both methods like:
所以我想知道是否可以将两种方法结合起来,例如:
def myFun3(name, lname, **other_info):
...
myFun3(lname='Someone', name='myName', city='cityName', otherInfo='blah')
In general what combinations can we do?
一般我们可以做哪些组合?
Thanks and sorry for my silly question.
感谢并抱歉我的愚蠢问题。
采纳答案by Gareth Latty
The general idea is:
总体思路是:
def func(arg1, arg2, ..., kwarg1=default, kwarg2=default, ..., *args, **kwargs):
...
You can use as many of those as you want. The *and **will 'soak up' any remaining values not otherwise accounted for.
您可以根据需要使用任意多个。在*和**将“吸收”任何剩余价值,未列入。
Positional arguments (provided without defaults) can't be given by keyword, and non-default arguments can't follow default arguments.
位置参数(不提供默认值)不能由关键字给出,非默认参数不能跟随默认参数。
Note Python 3 also adds the ability to specify keyword-only arguments by having them after *:
注意 Python 3 还增加了指定关键字参数的能力,方法是将它们放在 之后*:
def func(arg1, arg2, *args, kwonlyarg=default):
...
You can also use *alone (def func(a1, a2, *, kw=d):) which means that no arguments are captured, but anything after is keyword-only.
您也可以*单独使用( def func(a1, a2, *, kw=d):) 这意味着不捕获任何参数,但后面的任何内容都是关键字。
So, if you are in 3.x, you could produce the behaviour you want with:
所以,如果你在 3.x 中,你可以产生你想要的行为:
def myFun3(*, name, lname, **other_info):
...
Which would allow calling with nameand lnameas keyword-only.
这将允许使用name和lname作为仅关键字调用。
Note this is an unusual interface, which may be annoying to the user - I would only use it in very specific use cases.
请注意,这是一个不寻常的界面,可能会让用户感到恼火——我只会在非常特定的用例中使用它。
In 2.x, you would need to manually make this by parsing **kwargs.
在 2.x 中,您需要通过解析**kwargs.
回答by user2745509
It's possible at least for Python 2.7. Keyword arguments get assigned to positional parameters by name, so you can do
至少对于 Python 2.7 来说是可能的。关键字参数按名称分配给位置参数,因此您可以执行
In [34]: def func(name, lname, **kwargs):
print 'name='+name, 'lname='+lname
print kwargs
....:
In [35]: func(lname='lname_val', name='name_val', city='cityName', otherInfo='blah')
name=name_val lname=lname_val
{'city': 'cityName', 'otherInfo': 'blah'}
Official docs state it that way: "If keyword arguments are present, they are first converted to positional arguments, as follows. First, a list of unfilled slots is created for the formal parameters. If there are N positional arguments, they are placed in the first N slots. Next, for each keyword argument, the identifier is used to determine the corresponding slot (if the identifier is the same as the first formal parameter name, the first slot is used, and so on). If the slot is already filled, a TypeError exception is raised. Otherwise, the value of the argument is placed in the slot, filling it (even if the expression is None, it fills the slot)." https://docs.python.org/2/reference/expressions.html#calls
官方文档是这样说的:“如果存在关键字参数,它们首先被转换为位置参数,如下所示。首先,为形式参数创建一个未填充槽的列表。如果有 N 个位置参数,它们被放置在前N个槽位。接下来,对于每个关键字参数,通过标识符来确定对应的槽位(如果标识符与第一个形参名称相同,则使用第一个槽位,以此类推)如果槽位是已经填充,会引发 TypeError 异常。否则,将参数的值放入插槽中,填充它(即使表达式为 None,它也会填充插槽)。” https://docs.python.org/2/reference/expressions.html#calls
回答by balaji dileep kumar
You can add your named arguments along with kwargs. If the keys are available in the calling function It will taken to your named argument otherwise it will be taken by the kwargs dictionary.
您可以将命名参数与 kwargs 一起添加。如果调用函数中的键可用,它将采用您的命名参数,否则将采用 kwargs 字典。
def add(a=1, b=2,**c):
res = a+b
for items in c:
res = res + c[items]
print(res)
add(2,3)
5
5
add(b=4, a =3)
7
7
add(a =1,b=2,c=3,d=4)
10
10

