+ 2

Why I am getting error on code and not getting any error if I put '/ ' between name and **kwds.

I know that if we put / between parameters then the parameters left to the '/' will be treated as positional parameter by the python. 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:38 AM
lisa
5 Respostas
+ 2
By using / (positional only arguments), it is possible since it allows name as a positional argument and 'name' as a key in the keyword arguments
3rd Jun 2021, 11:09 AM
lisa
+ 1
I don't get what you say: could you provide code example wich does not raise error by 'putting / between parameters' ?
3rd Jun 2021, 9:56 AM
visph
visph - avatar
+ 1
#There is no errors in this code because we seperated pure positional arguments and keyword arguments. def foo(name, /, **kwds): return 'name' in kwds >>> foo(7, **{'name': 2}) True
3rd Jun 2021, 11:05 AM
lisa
+ 1
Wow! I didn't know that feature ^^ you make me learn something new ;) have you some sources about it (links)?
3rd Jun 2021, 11:10 AM
visph
visph - avatar
+ 1
# very nice! def fun(name,/,**kwargs): print(name) if 'name' in kwargs: print(kwargs['name']) fun('foo',name='bar') # dual name arguments fun('spam') # only name positional arg fun(name='egg') # throw error
3rd Jun 2021, 12:16 PM
visph
visph - avatar