7

A lot of inbuilt functions in python don't take keyword arguments. For example, the chr function.

>>> help(chr)
Help on built-in function chr in module builtins:

chr(i, /)
    Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.

Trying to pass values to chr using keyword arguments don't work.

>>> chr(i=65)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: chr() takes no keyword arguments

I know that the / character in the help text of the chr function means that it won't take keyword arguments.

How can I define a function that does not take keyword arguments? And of course, I want to define a function that takes arguments, but only positional arguments.

This will probably be marked as a duplicate but at least that way I'll get the answer. I can't find a StackOverflow answer for this question.

Another similar feature I learnt is to create a function that does not take positional arguments.

>>> def f(*, a, b):
...     print(a, b)
...

>>> f(1, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() takes 0 positional arguments but 2 were given

>>> f(a=1, b=2)
1 2

This question is similar to mine, but it doesn't actually answer my question. I still don't know how to define a function that will not accept keyword arguments, like several of the built-in functions.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Diptangsu Goswami
  • 5,554
  • 3
  • 25
  • 36
  • 2
    Possible duplicate of [python not accept keyword arguments](https://stackoverflow.com/questions/12333511/python-not-accept-keyword-arguments) – TrebledJ Jan 18 '19 at 10:42
  • That was found by copy-pasting your question title onto Google. :| Although admittedly, it's catered to Python 2 and not 3. – TrebledJ Jan 18 '19 at 10:42

2 Answers2

3

There's PEP 570, which is only a draft, so one cannot create positional-only arguments in pure Python. This can, however, be done in a function written in C for Python.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
1

Seeing as how the previous answer never got updated to 3.8 here's a brief answer for future readers

the / character in a function declaration marks all arguments before as positional only

def func(a, b, /):
    print(a ** b)

func(2, 4) # safe
func(a=2, b=4) # got some positional-only arguments passed as keyword arguments: 'a, b'

and the * character in a function declaration marks all arguments after as keyword only

def func(*, a, b):
    print(a ** b)

func(a=2, b=4) # safe
func(2, 4) # takes 0 positional arguments but 2 were given

these declarations can be combined to create a function with all three options positional only- default(both)- keyword only

def func(a, b, /, c, *, d, e):
    pass #too lazy to think of a use case

func(1, 2, 3, d=4, e=5) # safe
func(6, 7, c=8, d=9, e=10) # safe
func(1, b=2, c=3, d=4, e=5) # got some positional-only arguments passed as keyword arguments: 'b'
# etc
BlueLightning42
  • 344
  • 4
  • 15