Python - Positional-Only Arguments
It is possible in Python to define a function in which one or more arguments can not accept their value with keywords. Such arguments are called positional-only arguments.
To make an argument positional-only, use the forward slash (/) symbol. All the arguments before this symbol will be treated as positional-only.
Python’s built-in input() function is an example of positional-only arguments. The syntax of input function is −
input(prompt = "")
Prompt is an explanatory string for the benefit of the user. However, you cannot use the prompt keyword inside the parentheses.
Example
In this example, we are using prompt keyword, which will lead to error.
name = input(prompt="Enter your name ")
On executing, this code will show the following error message −<>
name = input (prompt="Enter your name ")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: input() takes no keyword arguments
Positional-Only Arguments Examples
Let’s understand positional-only arguments with the help of some examples −
Example 1
In this example, we make both the arguments of intr() function as positional-only by putting ”/” at the end.
def intr(amt, rate, /):
val = amt * rate / 100
return val
print(intr(316200, 4))
When you run the code, it will show the following result −
12648.0
Example 2
If we try to use the arguments as keywords, Python raises errors as shown in the below example.
def intr(amt, rate, /):
val = amt * rate / 100
return val
print(intr(amt=1000, rate=10))
On running this code, it will show following error message −
interest = intr(amt=1000, rate=10)
^^^^^^^^^^^^^^^^^^^^^^^
TypeError: intr() got some positional-only arguments passed as keyword arguments: 'amt, rate'
Example 3
A function may be defined in such a way that it has some keyword-only and some positional-only arguments. Here, x is a required positional-only argument, y is a regular positional argument, and z is a keyword-only argument.
def myfunction(x, /, y, *, z):
print (x, y, z)
myfunction(10, y=20, z=30)
myfunction(10, 20, z=30)
The above code will show the following output −
10 20 30
10 20 30