Positional-only and Keyword-only Arguments in Python (37/100 Days of Python)

Martin Mirakyan
4 min readFeb 7, 2023

--

Day 37 of the “100 Days of Python” blog post series covering positional-only and keyword-only arguments

Positional-only and keyword-only arguments are two unique argument-handling mechanisms in Python. They provide a way to specify which arguments should be passed to a function only by position (calling a function like f(1, 31) without specifying the keywords) and which should be passed only by keyword (calling a function like f(age=31, name='David')).

In Python, arguments can be passed to a function in two ways: positional arguments or keyword arguments:

  • A positional argument is an argument that is passed to a function based on its position in the argument list
  • A Keyword argument is passed to a function by specifying the parameter name and its corresponding value
def add(a, b):
return a + b

# Pass `a` as a positional argument and `b` as keyword argument
print(add(3, b=4))

Here, the argument 3 is passed to the add function as a positional argument, while the argument 4 is passed as a keyword argument. The first argument 3 is assigned to the a parameter and the second argument 4 is assigned to the b parameter.

The positional arguments are assigned to the function variables based on their order in the argument list, while keyword arguments are assigned to the function variables based on the names specified in the argument list.

Positional-only Function Arguments in Python

Positional-only arguments are a feature in Python that allows function parameters to be defined as positional-only, meaning they can only be passed to the function as positional arguments and cannot be passed as keyword arguments. This feature can be helpful in cases where the order of arguments is more important than their names, such as when processing images or performing geometry calculations. By using positional-only arguments, you can ensure that your functions are called with the correct arguments in the correct order, improving the clarity and maintainability of your code.

Positional-only arguments are separated with a forward slash (/) in the function definition. They can only be passed to a function as positional arguments and cannot be passed as keyword arguments. This can be useful in cases where the order of the arguments is more important than their names:

def rectangle_area(length, width, /):
return length * width

print(rectangle_area(10, 20)) # 200
print(rectangle_area(width=20, length=10)) # TypeError: rectangle_area() got some positional-only arguments passed as keyword arguments

Here, the rectangle_area function accepts two positional-only arguments length and width, which are passed directly to the function separated by a comma. We can also define a function that can accept both positional only and hybrid arguments:

def resize_image(height, width, /, name):
print(f'Resizing an image {name} with {height}x{width}')

resize_image(10, 20, 'thumbnail.png')
resize_image(1080, 1920, name='background.jpg')

Here the resize_image accepts two positional-only arguments and another argument name that can be both positional and keyword.

Keyword-only Function Arguments in Python

Keyword-only arguments are a feature in Python that allows function parameters to be defined as keyword-only, meaning they can only be passed to the function as keyword arguments and cannot be passed as positional arguments. This feature can be useful when you want to enforce the use of keyword arguments for certain parameters, such as optional parameters that have a default value, or when you want to clearly distinguish between mandatory and optional arguments.

Keyword-only arguments are specified after an asterisk (*) in the function definition. They can only be passed to a function as keyword arguments and cannot be passed as positional arguments. This can be useful in cases where certain arguments have a default value and should be optional.

Consider a function that calculates the volume of a cylinder. The radius and height are required arguments, while the units are optional. In this case, the units argument can be specified as keyword-only.

def cylinder_volume(radius, height, *, units='cm^3'):
return f'{3.14 * (radius ** 2) * height} {units}'

print(cylinder_volume(10, 20)) # 1256.0 cm^3
print(cylinder_volume(10, 20, units='mm^3')) # 1256000.0 mm^3
print(cylinder_volume(radius=10, height=20)) # 1256.0 cm^3
print(cylinder_volume(10, height=20, units='mm^3')) # 1256000.0 mm^3
print(cylinder_volume(10, height=20, 20)) # TypeError: cylinder_volume() takes 2 positional arguments but 3 were given

In this function, the parameters radius and height are defined as positional-only arguments, meaning they can only be passed to the function as positional arguments and cannot be passed as keyword arguments. The parameter units is defined as a keyword-only argument, meaning it can only be passed to the function as a keyword argument and cannot be passed as a positional argument.

What’s next?

--

--

Martin Mirakyan
Martin Mirakyan

Written by Martin Mirakyan

Software Engineer | Machine Learning | Founder of Profound Academy (https://profound.academy)

No responses yet