+ 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' >>>
5 Respuestas
+ 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
+ 1
I don't get what you say: could you provide code example wich does not raise error by 'putting / between parameters' ?
+ 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
+ 1
Wow!
I didn't know that feature ^^
you make me learn something new ;)
have you some sources about it (links)?
+ 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