Normal arguments vs Keywords arguments in Python
In this article, we will learn about the difference between a normal argument and a keyword argument in Python. We will use some custom code as well to better understand the differences. Let's first have a quick look over what is normal and keyword arguments in Python and then see a working example.
Functions in every language are used to define a behavior of an object. They are used to perform similar operations multiple times throughout the code. In order to access local variables inside a function, local variables are passed as arguments to the function using a function call. These arguments are simply known as Normal Arguments or Regular Arguments. The number of arguments in the function call statement and in the function definition should be equal.
The below example normally passes two values in the function call statement. The called function add() takes two parameters value1 and value2. These values are added inside the function scope and print the addition of two values.
def add(value1, value2): res = value1 + value2 print(res) #function call add(4,5)
Functions can take another type of argument known as Keyword Arguments. As the name suggests, it uses a keyword inside the function call statement to assign a value. This value is then used inside the function scope to perform the operation. You can pass the 'n' number of keywords with values according to your need.
The below example passes two keyword arguments with values in the function call statement of add(). These values are added inside the function scope and print the addition of two values. No argument value is assigned in the function definition.
def add(value1, value2): res = value1 + value2 print(res) #function call add(value1 = 4, value2 = 5)
Difference between Normal Arguments vs Keyword Arguments
Let us discuss the differences between the behaviors of these two arguments with help of examples.
1. In the case of Normal Arguments, only the value is passed to the function definition. The number of arguments during function call should be equal to the Parameters passed in the function definition. While in the case of Keyword Arguments, you pass the argument value along with the keyword during the function call.
#Normal Arguments def func1(v1,v2): add = v1 + v2 print(add) #prints 3 #Keyword Arguments def func2(v1,v2): add = v1 + v2 print(add) #prints 3 #function call func1(1,2) func2(v1 = 1, v2 = 2)
2. Order of arguments is an important thing to keep in mind while working with Normal Arguments. The first argument of the function call statement will direct to the first parameter inside the function definition. Order does not matter in the case of Keyword Arguments.
#Normal Arguments def func1(v1,v2): add = v1 + v2 print(add) #prints 3 #Keyword Arguments def func2(v1,v2,v3): add = v1 + v2 + v3 print(add) #prints 6 #function call func1(1,2) func2(v3 = 3, v1 = 1, v2 = 2)
3. Let us look at the different working examples of Keyword Arguments when you pass arguments during the function call.
Example: When Keywords are Inside Function Call Statement
This example takes the value of a as 2 and the value of b as 1 and passes these values to the called function. We declare keywords inside the function call statement.
def func(a, b): pass #function call func(a=2, b=1)
Example: When Keywords are Inside Function Call Statement as well as in the Function Definition
This example seems tricky but it is a convenient way to use Keyword Arguments. In Python, arguments passed inside the function call statement has more priority than the parameters assigned in the function definition. Here, keywords have values inside both function call and function definition. The function call works fine and prints 126.
def func(a="bar", b=5, c=123): print(b + c) #function call func(b=3, a="crab", c=123)
In this article, we learned about Normal and Keyword Arguments individually with the help of an example. We discussed the varying behavior of Keyword Argument depending upon the arguments passed. We discussed differences between the two arguments using custom examples.