What is the "/" parameter for when defining a function in Python?

Question:

I was contributing to a library on Github, and I came across the "/" parameter as a function parameter. The function is quite extensive and complex, so as not to be confused, I am going to create a simple one as an example.

def funcion_suma(a, b, /, c, d):
    return a + b + c + d

print(funcion_suma(1,2,3,4))

Departure: 10

For a second I thought it was a parameter, but no, the function has 4 parameters a , b , c and d . The slash (/) is simply performing some kind of operation.

Of course, if we define the function without the bar it works the same and gives the same result:

def funcion_suma(a, b, c, d):
    return a + b + c + d

print(funcion_suma(1,2,3,4))

Departure: 10

What is this type of syntax for?

Answer:

Explanation of the problem

It is a new syntax that can be used in function parameters. It has been added in Python 3.8 so it does not work in previous versions.

It is used to indicate that some parameters of the function must be specified exclusively positionally (postional argument) and CANNOT be passed as key arguments (keyword arguments)

In the above function, a and b are positional arguments, while c and d can be key or positional arguments. In summary, everything before the slash ( / ) becomes a positional argument only , the rest of the arguments after the slash retain the type of argument they are (positional, key, arbitrary).

In such a way that if in the previous function we try to pass a or b as key arguments, it 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, since they have been defined as exclusively positional arguments

Input:

def funcion_suma(a, b, /, c, d):
    return a + b + c + d

print(funcion_suma(1,2,c=3,d=4))

Output:

10

Possible use cases.

  • It allows functions that are written purely in Python to emulate behaviors of the C programming language which increases speed when making C and Python code compatible ( read PEP399 ).
  • Excluding the key arguments when the name of said parameter is not helpful, for example the function builtin len() , is defined by Python in the following way len(obj, /): #etc... so as not to induce with the parameter obj to confusion.
  • To be able to use these parameters as keywords in the event that 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 can be found in the documentation of improvement proposals for Python (PEP), specifically in PEP570 and also in a more summarized way in What's New In Python 3.8

Scroll to Top