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.
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.