Positional arguments and keyword arguments

A detailed explanation on positional arguments and keyword arguments in python

Chamanth mvs
AI Mind

--

Photo by Shahadat Rahman on Unsplash

Positional arguments

The most common way of assigning arguments to parameters is through the order in which they are passed in (that is through their position).

A function example_func() is defined with two parameters x and y

def example_func(x, y):
# body of the function
return None

When this function call is made, the values that are passed while making the function call are called arguments.

example_func(10,20)

The first argument (10) corresponds to the first parameter (x) and similarly, the second argument (20) corresponds to the second parameter (y) i.e., example_func(10, 20) param_1 = 10 and param_2 = 20

Similarly, example_func(20, 10) x = 20 and y = 10

Positional arguments look at the position of the argument and assign it to the corresponding parameter at the same position

A positional argument can be made optional by specifying the default value for corresponding parameter.

def example_func(x, y=77):
print(f"The value of x is {x}")
print(f"The value of y is {y}")
return None

In the above function, for parameter y, a default value is specified. So, when a function call is made without specifying about y, the value of y would be set for 77.

example_func(40)

output : The value of x is 40
The value of y is 77

Even if the y value is not specified in the function call, how did it run? — without raising any error. It worked because for the argument y, the value is already set to 77. So, python used it to run the function successfully, without any errors.

example_func(40, 90)

output : The value of x is 40
The value of y is 90

When the function call is made, the default values for the parameters will not work because, the values are already being passed as arguments in the function call. Python would interpret the values that are passed in as arguments.

Think on

Consider three arguments to be passed on to the function, with one of them being optional

def example_func(x, y=77, z):
print(f"The value of x is {x}")
print(f"The value of y is {y}")
print(f"The value of z is {z}")
return None

x, y and z are three parameters and the default value has been specified for y, wanted to make a function call —example_func(5,25) — as per the above explanation, if there is already a default value for the parameter, then it is optional to pass an argument corresponding to it.

example_func(5,25) — python, considers 5 as an argument corresponding to x and then, what about 25?

example_func(5,25)

output : ERROR

python confuses to make it correspond to y or z? So, it results in an ERROR and a function cannot be defined in such way def example_func(x, y=77, z) — it would be an SYNTAX ERROR

error relating to positional arguments

If a positional parameter is defined with a default value in the function definition, then every positional parameter after it must also be given a default value

In the above example, when a default value of y is defined as 88, then the positional parameter following y (which is z) should also have some default value (as stated below)

def example_func(x, y=77, z=33):
print(f"The value of x is {x}")
print(f"The value of y is {y}")
print(f"The value of zis {z}")
return None

In this function — example_func, there is default value for y as well as for z

example_func(2) — the parameter x corresponds to 2 and the remaining parameters have their default values

example_func(5)

output : The value of x is 5
The value of y is 77
The value of zis 33

example_func(2, 28) — the parameter x corresponds to 2 and the parameter y corresponds to 28 and the remaining parameters have their default values

example_func(5, 28)

output : The value of x is 5
The value of y is 28
The value of zis 33

example_func(2, 28, 10) — the parameter x corresponds to 2 and the parameter y corresponds to 28 and the parameter z corresponds to 10

example_func(5, 28, 10)

output : The value of x is 5
The value of y is 28
The value of zis 10

Think on

If we want to specify the 1st and 3rd arguments in a function by omitting the 2nd argument?

In this function — example_func(x, y, z) — if we want to specify arguments for x and z but not to y — — this case is NOT possible with positional-arguments, this is where keyword-arguments comes into action.

Keyword arguments (named arguments)

Name of the parameter needs to be specified when an argument value is specified

When making a function call, the names provided with arguments must correspond to the names of the parameter in the function definition.

example_func(x=5, y=3, z=1)

A function call can have both positional arguments as well as named arguments.

example_func(7, y=4, z=3)

With the help of keyword arguments, the positional arguments can optionally be specified using the parameter name.

Why should named arguments to be used, even if the function is having non-default parameters? — because, the order in which arguments are passed to the function doesn’t matter.

example_func(y=4, z=8, x=2)

In the above example, the arguments are passed in different order, than it is specified in the function definition.

If the named-arguments are used, then all the arguments that are used after the named-arguments must also be named too

example_func(z=10, 3, 1)

output : ERROR

python will get confused whether 3 and 1 corresponds to x and y (or) does it correspond to y and x. So, it will be SYNTAX ERROR.

example_func(9, y=2, 7)

output : ERROR

This will also be an Error because python confuses whether 9 and 7 corresponds to x and y (or) does it correspond to y and x. So, it will also be a SYNTAX ERROR.

Once a keyword-argument (named-argument) is being used, then you must use keyword-argument thereafter for every other argument that comes.

example_func(6, y=2, z=3)

This will work because, python assumes 6 to be a positional argument to the function — example_func 2 and 3 being keyword-argument to the function example_func.

--

--

Data Science and ML practitioner | I share my learnings and thoughts here