What are Positional-only Parameters and how to use them in Python 3.8?
If you have prior experience in any programming then you are very well aware of passing parameters into the function. But commonly most programming languages follow the rule of passing and assigning passed values to function arguments in positional bases.
Suppose if you have a function add_numbers(num1, num2)
which takes two integers as parameters that naturally the first parameter belong to num1
and the second parameter belong to num2
and this is how it must work.
But Python programming standards before followed assigning passed variable to named parameters. Using positional parameters with named arguments created confusion among developers so python programmers in version 3.8 included positional-only parameters that can be used with other keyword or named parameters.
Table of Contents
Introduction
In Position-only we pass variables to functions by putting it into their respective positions rather than naming then with the parameter name, unlike named arguments.
If we interchange variable positions then the variables will be passed to other parameters.
If you try to pass a value to a positional argument using the named argument passing method then your script will throw the below exception.
TypeError: got some positional-only arguments passed as keyword arguments
A simple example of Positional-only Parameter
Below is the simple example function my_func
takes three parameters a, b, c
and to tell python that these arguments must be considered as the positional one we use a forward slash /
symbol.
def my_func(a, b, c, /): print(f""" a : {a} \n b : {b} \n c : {c} \n """); my_func(11,12,10) #PYTHON OUTPUT a : 11 b : 12 c : 10

Python position-only-parameter function with forwarding slash
And if we try to access my_func
function by passing named arguments than python throws exception shown below.
my_func(a=11,b=12,c=10) # when added / the throws TypeError: my_func() got some positional-only arguments passed as keyword arguments: 'a, b, c'
Positional with named arguments
We can also use both positional and named argument in Python version 3.8.
def test_func(a,b,c,/,**kwargs): print(f""" a : {a} \n b : {b} \n c : {c} \n d : {d} \n e : {e} \n """); test_func(10,11,12,d=13,e=14) #PYTHON OUTPUT a : 10 b : 11 c : 12 d : 13 e : 14

Using Python position-only-parameter function with named arguments
In function test_func
the parameters before forward slash /
are positional parameters and after forward-slash they are keywords parameters.
test_func(10,11,12,13,14) #works test_func(10,11,12,d=13,e=14) #works test_func(10,11,12,e=14,d=13) #works even if we switch position on d and e test_func(10,11,c=12,e=14,d=13) #does not work throws got some positional-only arguments passed as keyword arguments: 'c'
It also works if we pass values to all the parameters without naming them and also we can pass by only naming parameter d and e
as they are named argument/parameters.
And if we pass parameter c through named argument way then an exception will be thrown because c is a positional-only parameter.
Insisted on accessing named arguments as d and e. We can use **kwargs
to access them the above function test_func
is modified
and recreated below and that also works the same way as the above program.
def test_func(a,b,c,/,**kwargs): print(f""" a : {a} \n b : {b} \n c : {c} \n d : {kwargs['d']} \n e : {kwargs['e']} \n """); #PYTHON OUTPUT a : 10 b : 11 c : 12 d : 13 e : 14
Conclusion
We have come to the final part of our post What are Positional-only Parameters in Python 3.8. we want to thank you for reading our post. If you like this share post and provide your suggestions in the comments.
Related Posts
- Python Local, Global Variables and Globals Function for Beginners with Examples
- Python Types of Methods | Instance, Class, and Static Methods for Beginners




