Python – Positional-Only Arguments

What are Positional-Only Arguments in Python?

In Python, positional-only arguments are a feature introduced in Python 3.8 that allow you to specify arguments that can only be passed positionally, without the use of keyword arguments. This means that these arguments cannot be specified using their parameter names.

How to Define Positional-Only Arguments

To define a positional-only argument in a Python function, you can use the forward slash (“/”) in the function’s parameter list. All parameters before the forward slash are considered positional-only.

Here’s an example:

def greet(name, /, age):
    print(f"Hello {name}, you are {age} years old.")

In the above example, the “name” parameter can be passed either positionally or using a keyword argument, while the “age” parameter can only be passed positionally.

Using Positional-Only Arguments

When calling a function that has positional-only arguments, you can only pass values for those arguments positionally, without using their parameter names.

Here’s an example:

greet("Alice", 25)

The above code will output:

Hello Alice, you are 25 years old.

If you try to pass the “age” argument using a keyword, you will get a TypeError:

greet(name="Alice", age=25)  # This will raise a TypeError

Benefits of Positional-Only Arguments

Positional-only arguments can be useful in certain situations where you want to enforce a specific calling convention or improve the readability of your code.

Here are a few benefits of using positional-only arguments:

  • Improved Readability: By making certain arguments positional-only, you can make the function calls more concise and readable, as the caller doesn’t need to specify the parameter names for those arguments.
  • Forced Positional Arguments: Positional-only arguments can be used to force the caller to provide values for certain arguments positionally, without allowing them to use keyword arguments. This can be useful when you want to ensure that certain arguments are always provided in a specific order.
  • API Design: Positional-only arguments can be used to design APIs that are more intuitive and self-explanatory, as the caller doesn’t need to remember or specify the parameter names for certain arguments.

Limitations of Positional-Only Arguments

While positional-only arguments can be useful, it’s important to note that they have some limitations:

  • Backward Compatibility: Positional-only arguments are only available in Python 3.8 and later versions. If you need to support older versions of Python, you cannot use this feature.
  • Less Flexibility: By making an argument positional-only, you restrict the caller’s ability to pass values using keyword arguments. This can be a limitation in some cases where the caller may want to specify certain arguments by name.

Conclusion

Positional-only arguments in Python provide a way to specify arguments that can only be passed positionally, without using keyword arguments. They can improve the readability of function calls and enforce a specific calling convention. However, it’s important to consider the limitations and the compatibility of this feature when using it in your code.

Scroll to Top