+ 1

what is the purpose to keep the 1st parameter name ?is there any role of 1st arg 7 ,1st positional parameter name in this code ?

def foo(name, **kwds): return 'name' in kwds >>> foo(7, **{'name': 2}) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: foo() got multiple values for argument 'name' >>>

3rd Jun 2021, 8:30 AM
lisa
6 Antworten
+ 2
what you do with foo(7,**{'name':2}) is equivalent to foo(name=7,name=2)
3rd Jun 2021, 8:44 AM
visph
visph - avatar
+ 2
explicit positional arguments (as opposed to optional: only a name) are required... you cannot unpack a dict with a key equal to it if you provide a positional value, but you can pass it by providing its name or by unpacking a dict with a key with same name without providing the positional one: foo(42) foo(**{'name':42}) foo(name=42) all three are equivalent and working ;)
3rd Jun 2021, 8:43 AM
visph
visph - avatar
+ 1
visph Yes I understand.
3rd Jun 2021, 10:55 AM
lisa
0
Manohar Yadav she doesn't pass the dictionary, she unpack it (and keys wich are not explictly declared in function signature are re-packed to another dictionary)
3rd Jun 2021, 8:47 AM
visph
visph - avatar
0
visph What do you mean by this ? "keys which are not explictly declared in function signature are re-packed to another dictionary."
3rd Jun 2021, 10:58 AM
lisa
0
function signature is the 'def' line: what make it unique, name and most important the argumznt list... in your code example the 'explicitly declared' key/argument is the 'name' argument wich cannot be found as key in the kwds dictionary ;)
3rd Jun 2021, 11:03 AM
visph
visph - avatar