Keyword and Positional Argument in Python
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
|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 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:
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:
Hi, I am Prince My age is 20 Hi, I am Prince My age is 20
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 : –
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.
Case-1: Hi, I am Prince My age is 20 Case-2: Hi, I am 20 My age is Prince
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.
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.
Please Login to comment...