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.
Keyword arguments aren't just useful for functions that accept any number of positional arguments (like print ). You can pass keyword arguments to just about any function in Python. We're passing in one positional argument and one keyword argument. That start=1 works with sum because start is the name of that argument.
Keyword arguments are passed to functions after any required positional arguments. But the order of one keyword argument compared to another keyword argument does not matter.
A positional argument is a name that is not followed by an equal sign (=) and default value. A keyword argument is followed by an equal sign and an expression that gives its default value.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With