Python provides different ways of passing the arguments during the function call from which we will explore keyword-only argument means passing the argument by using the parameter names during the function call.isĀ
Types of arguments
- Keyword-only argument
- Positional-only argument
Difference between the Keyword and Positional Argument
Keyword-Only Argument |
Positional-Only Argument |
---|---|
Parameter Names are used to pass the argument during the function call. | Arguments are passed in the order of parameters. The order defined in the order function declaration. |
Order of parameter Names can be changed toĀ pass the argument(or values). |
Order of values cannot be changed to avoidĀ the unexpected output. |
Syntax : āĀ | Syntax :- |
Keyword-Only Arguments
Keyword-only arguments mean whenever we pass the arguments(or value) by their parameter names at the time of calling the function in Python in which if you change the position of arguments then there will be no change in the output.
Benefits of using Keyword arguments over positional arguments
- On using keyword arguments you will get the correct output because the order of argument doesnāt matter provided the logic of your code is correct. But in the case of positional arguments, you will get more than one output on changing the order of the arguments.
Letās see the example for the keyword-only argument as explained below:
Example
So now, we will call the function by using the keyword-only arguments(or by using the parameter names) in two ways and In both cases, we will be getting the correct output but not necessarily in the positional argument. Here we have used argument naming to declare the value. This is called the keyword-only argument. Letās assume we have a function that tells the name and age of the individual(or person) as defined below:
Python3
def nameAge(name, age): Ā Ā Ā Ā print ( "Hi, I am" , name) Ā Ā Ā Ā print ( "My age is " , age) Ā
nameAge(name = "Prince" , age = 20 ) Ā
nameAge(age = 20 , name = "Prince" ) |
Hi, I am Prince My age is 20 Hi, I am Prince My age is 20
Positional-Only Arguments
Position-only arguments mean whenever we pass the arguments in the order we have defined function parameters in which if you change the argument position then you may get the unexpected output. We should use positional Arguments whenever we know the order of argument to be passed. So now, we will call the function by using the position-only arguments in two ways and In both cases, we will be getting different outputs from which one will be correct and another one will be incorrect.
Letās see the examples for the positional-only Arguments as explained below : ā
Example 1
During the function call, we have used the Position such that the first argument(or value) will be assigned to name and the second argument(or value) will be assigned to age. By changing the position or in case you forgot the order of the position then the values can be used at the wrong places as we can see in the below example of Case-2 that 20 has been assigned to the name and Prince has been assigned to age.
Python3
def nameAge(name, age): Ā Ā Ā Ā print ( "Hi, I am" , name) Ā Ā Ā Ā print ( "My age is " , age) Ā
# You will get correct output because argument is given in order print ( "Case-1:" ) nameAge( "Prince" , 20 ) # You will get incorrect output because argument is not in order print ( "\nCase-2:" ) nameAge( 20 , "Prince" ) |
Case-1: Hi, I am Prince My age is 20 Case-2: Hi, I am 20 My age is Prince
Example 2
During the calling of the function, we have used the position to pass the argument(or value) such that first value will be assigned to a and second value will be assigned to b. But in the result2 you will getting the incorrect output because in the function declaration b should be subtracted from the a but in result2 a is been subtracted from b because of the swapped position.
Python3
def minus(a, b): Ā Ā Ā Ā return a - b Ā
Ā
a, b = 20 , 10 result1 = minus(a, b) print ( "Used Positional arguments:" , result1) Ā
# you will get incorrect output because # expected was (a-b) but you will be getting (b-a) # because of swapped position of value a and b Ā
result2 = minus(b, a) print ( "Used Positional arguments:" , result2) |
Used Positional arguments: 10 Used Positional arguments: -10
Note: We should use the positional argument if we know the order of arguments or before using read the function you use and know the order of arguments to be passed over there otherwise you can get the wrong output as you can see in above-explained example for positional Argument.