Positional-only arguments in Python

Positional-only arguments in Python

An introduction to the new `/` syntax in Python 3.8.

Originally published by Sanket at https://deepsource.io

The ability to specify positional-only arguments using the / marker in function definitions is among the many new improvements to the Python language coming in the upcoming 3.8 release. This addition to syntax has performance benefits and enables better API design. Let's look at the motivation behind positional-only arguments and how to use it, with examples. 

Background

Keyword-only arguments have been available in Python with the * marker, and addition of / marker for positional-only arguments improves the language’s consistency. With positional-or-keyword parameters, the mix of calling conventions is not always desirable. Consider these examples:

  • Some function parameters already have semantic meaning: namedtuple(typenames, field_names, …)
  • The parameter name has no true external meaning: arg1, arg2, …, etc for min()

If the users start using a keyword argument, the library author cannot rename the parameter because it would be a breaking change. In case of min(), the name of the parameter provides no intrinsic value and forces the author to maintain its name forever since callers might pass arguments as a keywords. This problem is solved by positional-only arguments. In addition, the parsing and handling of positional-only arguments is faster.

How to use positional-only arguments

To specify arguments as positional-only, a / marker should be added after all those arguments in the function definition. Take this example:

def pow(x, y, /, mod=None):
   r = x ** y
   if mod is not None:
       r %= mod
   return r

The following would apply:

  • All parameters to the left of / (in this case, x and y) can only be passed positionally.
  • mod can be passed positionally or with a keyword.
>>> pow(2, 10)  # valid

>>> pow(2, 10, 17)  # valid

>>> pow(2, 10, mod=17)  # valid

>>> pow(x=2, y=10)  # invalid, will raise a TypeError

>>> pow(2, y=10)  # invalid, will raise a TypeError

Another example:

def table_format(items, separator=',', /, prettify=False):

    pass

  • Once a positional-only parameter is specified with a default, the following positional-only and positional-or-keyword parameters need to have defaults as well (in this case, prettify).
  • Positional-only parameters which do not have default values are required positional-only parameters (in this case, items).

The generic case

A function definition, with all flavors of argument-passing variants, would look like this:

def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):

      -----------    ----------     ----------

        |             |                  |

        |        Positional or keyword   |

        |                                - Keyword only

         -- Positional only

When to use positional-only arguments

  • Use positional-only if names do not matter or have no meaning, and there are only a few arguments which will always be passed in the same order.
  • Use keyword-only when names have meaning and the function definition is more understandable by being explicit with names.

Positional-only arguments were proposed in PEP 570, and it’s worth talking a look at the doc to understand the feature in more detail.

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading about Python

Complete Python Bootcamp: Go from zero to hero in Python 3

Machine Learning A-Z™: Hands-On Python & R In Data Science

Python and Django Full Stack Web Developer Bootcamp

Complete Python Masterclass

Python Tutorial - Python GUI Programming - Python GUI Examples (Tkinter Tutorial)

Computer Vision Using OpenCV

OpenCV Python Tutorial - Computer Vision With OpenCV In Python

Python Tutorial: Image processing with Python (Using OpenCV)

A guide to Face Detection in Python


python web-development machine-learning

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Hire Python Developer | Python web development company india

Hire Python Developer from us for Scalable, Secure & Robust Python Web development Solutions. Strict NDA | 16+ Years Exp| 2500+ Clients| 450+ Experts

Hire Python Developer | Python web development company india

Hire Python Developer from us for Scalable, Secure & Robust Python Web development Solutions. Strict NDA | 16+ Years Exp| 2500+ Clients| 450+ Experts

Hire Machine Learning Developers in India

We supply you with world class machine learning experts / ML Developers with years of domain experience who can add more value to your business.

Hire Machine Learning Engineer | Offshore Machine Learning Experts

We are a Machine Learning Services provider offering custom AI solutions, Machine Learning as a service & deep learning solutions. Hire Machine Learning experts & build AI Chatbots, Neural networks, etc. 16+ yrs & 2500+ clients.

Hire Machine Learning Developer | Hire ML Experts in India

We supply you with world class machine learning experts / ML Developers with years of domain experience who can add more value to your business.