In the previous post, we discussed about Positional Arguments in Python. In this post, let's look at Keyword Arguments in Python. We will reuse the function print_stats.
def print_stats(name ="Sachin Tendulkar",age = 49,type_batsman = "Right", avg_runs=44.8):
print(f"Name of cricketer is {name}.")
print(f"Age of {name} is {age} years.")
print(f"{name} is {type_batsman} handed bastsman.")
print(f"Career ODI Average for {name} is {avg_runs} runs.")
print_stats()
--->
Name of cricketer is Sachin Tendulkar.
Age of Sachin Tendulkar is 49 years.
Sachin Tendulkar is Right handed bastsman.
Career ODI Average for Sachin Tendulkar is 44.8 runs.
Keyword arguments come in the form kwarg=value. Notice, how we have given values for each of our function arguments (name ="Sachin Tendulkar" ,age = 49, type_batsman = "Right", avg_runs=44.8). If they are defined in the function definition, those values become default values in the function. As we see above, although no argument was passed to the print_stats function, it took on default values available in the function definition. Now, let's say, I want to print statistics for some other cricketer. This is how it is done.
def print_stats(name ="Sachin Tendulkar",age = 49,type_batsman = "Right", avg_runs=44.8):
print(f"Name of cricketer is {name}.")
print(f"Age of {name} is {age} years.")
print(f"{name} is {type_batsman} handed bastsman.")
print(f"Career ODI Average for {name} is {avg_runs} runs.")
print_stats(name ="Sourav Ganguly",age = 50,type_batsman = "Left", avg_runs=41)
--->
Name of cricketer is Sourav Ganguly.
Age of Sourav Ganguly is 50 years.
Sourav Ganguly is Left handed bastsman.
Career ODI Average for Sourav Ganguly is 41 runs.
Now let's revert to function defintion of print_stats as per the previous post. We will learn some new concepts. This function will have only positional arguments.
def print_stats(name,age,type_batsman, avg_runs):
print(f"Name of cricketer is {name}.")
print(f"Age of {name} is {age} years.")
print(f"{name} is {type_batsman} handed bastsman.")
print(f"Career ODI Average for {name} is {avg_runs} runs.")
print_stats(name ="Sourav Ganguly",age = 50,type_batsman = "Left", avg_runs=41)
--->
Name of cricketer is Sourav Ganguly.
Age of Sourav Ganguly is 50 years.
Sourav Ganguly is Left handed bastsman.
Career ODI Average for Sourav Ganguly is 41 runs.
We can see that, the function can be called using keyword arguments even if it is defined in terms of positional arguments . Now, let's modify the way we are passing the arguments to the function. As you can see below, the order of the arguments passed to function does not matter when utilizing keyword arguments.
def print_stats(name,age,type_batsman, avg_runs):
print(f"Name of cricketer is {name}.")
print(f"Age of {name} is {age} years.")
print(f"{name} is {type_batsman} handed bastsman.")
print(f"Career ODI Average for {name} is {avg_runs} runs.")
print_stats(age = 50, name ="Sourav Ganguly", avg_runs=41, type_batsman = "Left")
--->
Name of cricketer is Sourav Ganguly.
Age of Sourav Ganguly is 50 years.
Sourav Ganguly is Left handed bastsman.
Career ODI Average for Sourav Ganguly is 41 runs.
However, if we do not pass some of the arguments as keywords, it will throw an error. Notice below that I have changed the function signature slightly. The name argument no longer has a default value.
def print_stats(name ,age = 49,type_batsman = "Right",avg_runs = 44.8):
print(f"Name of cricketer is {name}.")
print(f"Age of {name} is {age} years.")
print(f"{name} is {type_batsman} handed bastsman.")
print(f"Career ODI Average for {name} is {avg_runs} runs.")
print_stats(age = 50,type_batsman = "Left")
--->
--------------------------------------------------------------------------
TypeError Traceback (most recent call last)
-->ipython-input-25-d52d720da640> in module
----> 1 print_stats(avg_runs=44.8 , type_batsman="Right", age=49)
TypeError: print_stats() missing 1 required positional argument: 'name'
Clearly, value for the name argument was missing when I was trying to call the print_stats function. If I had given a default value to name it would not have thrown a TypeError. But , we should be careful while using default values and be sure how it is being utilized.
Next up, what if I want to restrict the function to not accept positional arguments. Can this be done? Sure!!. Just add a * before the function arguments to not allow positional arguments. Let's see this below. Notice, how we modify the arguments to the function definition.
def print_stats(*,name , age, type_batsman, avg_runs):
print(f"Name of cricketer is {name}.")
print(f"Age of {name} is {age} years.")
print(f"{name} is {type_batsman} handed bastsman.")
print(f"Career ODI Average for {name} is {avg_runs} runs.")
print_stats(name ="Sourav Ganguly",age = 50,type_batsman = "Left", avg_runs=41)
--->
Name of cricketer is Sourav Ganguly.
Age of Sourav Ganguly is 50 years.
Sourav Ganguly is Left handed bastsman.
Career ODI Average for Sourav Ganguly is 41 runs.
print_stats("Sourav Ganguly", 50, "Left", 41)
--->
TypeError Traceback (most recent call last)
ipython-input-7-b1773a83ac9d in module
----> 1 print_stats("Sourav Ganguly", 50, "Left", 41)
TypeError: print_stats() takes 0 positional arguments but 4 were given
As we can see, TypeError is thrown if we try to pass positional arguments to this function.
This concludes the post. In the next post, we will combine ideas from this post and the previous post to understand some important concepts on *args and **kwargs.
Comments
Post a Comment