Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a python function which take only positional arguments and no keyword arguments?

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.

like image 563
Diptangsu Goswami Avatar asked Jan 18 '19 10:01

Diptangsu Goswami


People also ask

Can positional arguments be used with keyword arguments Python?

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.

Can positional arguments be used with keyword arguments?

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.

What is the difference between positional arguments and keyword argument in Python?

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.


2 Answers

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.

like image 188
ForceBru Avatar answered Oct 23 '22 13:10

ForceBru


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
like image 20
BlueLightning42 Avatar answered Oct 23 '22 13:10

BlueLightning42