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.

3
  • 2
    Possible duplicate of python not accept keyword arguments Commented Jan 18, 2019 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. Commented Jan 18, 2019 at 10:42
  • … and, why would you want to do this? PEP570 seems to imply that this would allow you to use parameter names which don’t have to make much sens on the outside. Commented Jan 31, 2024 at 23:49

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I suppose this feature is coming to python 3.8
@DiptangsuGoswami, since the PEP has been accepted, it sure is!
Now that this feature is part of python, could you please edit your answer and add an example for this feature?
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.