Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
95 views
in Technique[技术] by (71.8m points)

python - Got an unexpected keyword argument

Using Kwargs for unpacking elements from dictionary. Intrepreter throws unexpected keyword argument error.

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 }))

Error Msgs :

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'
question from:https://stackoverflow.com/questions/65839501/got-an-unexpected-keyword-argument

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I guess you are confusing between positional arguments to keyed arguments.

When unpacking a dictionary python match between the dictionary key xx to a named argument that is called xx but there isn't one.

if you want to unpack positional argument use *[4, 1, 3] and it will be mapped to x=4, y=1, z=3


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...