Positional Only Arguments In Python

  • Available in 3.8 and later
  • Positional-only can never be passed with a keyword
  • Any attempt to pass a keyword arg will result in a TypeError
def number_length(x,/):
return len(str(x))
>>> number_length("abcdef")
6

>>> number_length(x="abcdef")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: number_length() got some positional-only arguments passed as keyword arguments: 'x'

number_length() got some positional-only arguments passed as keyword arguments: 'x'
  1. Prevent certain argument names from becoming part of the API; this prevents dependencies on the names which is wseful when the names have no semantic meaning

LINKS

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (Snowplow Analytics) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com