J
Explanation of the problemIt's a new syntax. which can be used in the function parameters. It's been added to Python 3.8. so it doesn't work in previous versions.It serves to indicate that some function parameters should be specified exclusively positionally (postional argument) and cannot be passed as key arguments (keyword arguments)In the previous function, a and b are positional arguments, while c and d can be key or positional arguments. In summary everything before the bar (/) becomes a single positional argumentthe other arguments after the bar retain the type of argument that they are (positional, key, arbitrary).So if in the previous function we try to pass a or b as key arguments, will give us the following TypeError:Input:def funcion_suma(a, b, /, c, d):
return a + b + c + d
print(funcion_suma(a=1,b=2,c=3,d=4))
Output:TypeError: funcion_suma() got some positional-only arguments passed as keyword arguments: 'a, b'
However, if we pass a and b as positional arguments, everything is correct, as they have been defined as exclusively positional argumentsInput:def funcion_suma(a, b, /, c, d):
return a + b + c + d
print(funcion_suma(1,2,c=3,d=4))
Output:10
Possible cases of use.Allows functions that are written purely on Python to emulate programming language behaviors C which causes the speed to increase when compatibility of code C and in Python ( https://www.python.org/dev/peps/pep-0399/ ).Exclude key arguments when the name of the parameter is not helpful, for example the builtin function len(), is defined by Python as follows len(obj, /): #etc... to not induce with the parameter obj to confusion.To be able to use such parameters as keywords in case it is necessary in the future, for example:def f(a, b, /, **kwargs):
print(a, b, kwargs)
print(f(10, 20, a=1, b=2, c=3)) #Usamos el parámetro "a" y "b" como keyword ya que nunca ha sido usado antes.
Output:10 20 {'a': 1, 'b': 2, 'c': 3}
All this information and more, it is in the documentation of improvement proposals for Python (PEP), specifically in https://www.python.org/dev/peps/pep-0570/ and also in a more summary form https://docs.python.org/3/whatsnew/3.8.html