得到一个意想不到的关键字参数

Got an unexpected keyword argument

使用 Kwargs 从字典中解包元素。解释器抛出意外的关键字参数错误。

def f (x, y, z) : return x + y * z

y = lambda x, y, z : x+y+z

print(f(**{ 'z' : 4 , 'x' : 1 , 'y' : 3 }))

print(y(**{ 'z' : 4 , 'x' : 1 , 'y' : 3 }))

print(f(**{ 'z' : 4 , "xx" : 1 , 'y' : 3 }))

print(y(**{ 'z' : 4 , 'xx' : 1 , 'y' : 3 }))

错误消息:

13
8
Traceback (most recent call last):
  File "C:/Python39/test.py", line 8, in <module>
    print(f(**{ 'z' : 4 , "xx" : 1 , 'y' : 3 }))
TypeError: f() got an unexpected keyword argument 'xx'

我猜你混淆了位置参数和键控参数。

解压字典时 python 字典键 xx 与名为 xx 的命名参数之间的匹配,但没有一个。

如果你想解压位置参数使用 *[4, 1, 3] 它将被映射到 x=4, y=1, z=3