Python is a high-level, interpreted programming language and is one of the most popular programming languages present in the software industry.
One of the most important concepts in python is that of keyword arguments used in functions. Let us have a look at it in detail.
In python, arguments are used in functions to pass on specific information when they are called so that their functionality can be tailored according to requirements.
Arguments are often termed as parameters as well. One type of argument is keyword arguments.
These are used when you do not want to worry about the order in which you pass the arguments.
One downside to this is that it increases the amount of code that needs to be written just by a little bit.
The syntax to specify keyword arguments in python using functions is pretty straightforward and is as follows.
When defining the function, we specify the name of the argument and if we have multiple arguments we separate them using commas.
Do keep in mind the indentations required to specify the function body.
def <function_name>(<argument_name1>, <argument_name2>, …..):
<statements>
To call the functions using keyword arguments, the syntax is as follows:
<function_name>(<argument_name1> == <value1>, <argument_name2> == <value2>, ….)
This is a very simple syntax however there are some rules one needs to keep in mind when specifying arguments.
The order of arguments in python is very important both while defining and calling functions but when using keyword arguments during the call, we do not need to worry about the order as long as the name of the argument and desired value passed in is correct.
One important rule to remember is that when calling a function, keyword arguments cannot be specified before positional arguments.
In this example, we look at the usage of the concept of keyword arguments in a function call that helps us to calculate the sum of two given arguments.
One unusual thing here is that we do not need to worry about the order of the arguments passed in during function calls.
Here we have a function named getSum() that takes in two arguments a and b.
It stores the sum of these arguments in a variable named sum and returns it.
We call this function first using two variables x and y, with respective argument names as well, containing values 3 and 2 respectively.
As expected the result is stored in a variable z and on printing it we see an output of 5 which is the sum of 3 and 2.
# Function with arguments a and b
def getSum(a, b):
# Variable to store the sum
sum = a + b
# Returning the sum
return sum
x = 3
y = 2
# Calling function with arguments
z = getSum(x,y)
# Display result
print(z)
# Calling function by specifying the arguments key
z = getSum(a = x,b = y)
print(z)
Output
5
5
Now let us say we need to interchange the order in which the arguments are passed and pass b first followed by a, both as keyword arguments.
The output remains unchanged and we receive 5 as the output on printing the returned value after storing it in a variable named z.
Let us take a look at the python code and corresponding output for this method:
# Function with arguments a and b
def getSum(a, b):
# Variable to store the sum
sum = a + b
# Returning the sum
return sum
x = 3
y = 2
# Calling function with keyword arguments
z = getSum(b = y, a = x) # arguments order is changed
# Display result
print(z)
Output
5
As mentioned earlier we cannot have keyword arguments before positional arguments in a function call.
In this example, we have the same function getSum() as before to return the sum of two arguments but a difference here is that only a is a keyword argument with a value of x whereas b is a positional argument.
As we know, having keyword arguments before positional ones are not allowed in python and we get an error stating the same.
Let us look at the python code and corresponding output for this example:
# Function with arguments a and b
def getSum(a, b):
# Variable to store the sum
sum = a + b
# Returning the sum
return sum
x = 3
y = 2
# Calling function with keyword arguments
z = getSum(a = x, y)
# Display result
print(z)
Output
SyntaxError: positional argument follows keyword argument
Apart from the above scenario, these are some wrong functions called in keyword arguments. You will get a compile-time error if you ever call the function in such a manner.
z=getSum() # TypeError: getSum() missing 2 required positional arguments: 'a' and 'b'
print(z)
Output:
TypeError: getSum() missing 2 required positional arguments: 'a' and 'b'
z=getSum(a=1000,12) # SyntaxError: positional argument follows keyword argument
print(z)
Output:
SyntaxError: positional argument follows keyword argument
z=getSum(10000, a=200) # TypeError: getSum() got multiple values for argument 'a'
print(z)
Output:
TypeError: getSum() got multiple values for argument 'a'
z=getSum(c=200) # TypeError: getSum() got an unexpected keyword argument 'c'
print(z)
Output:
TypeError: getSum() got an unexpected keyword argument 'c'
In this topic, we have learned the use and advantages of keyword arguments in a Python program, following some simple running examples, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to info.javaexercise@gmail.com in case of any suggestions.