Skip to main content

Standard Normal Distribution with examples using Python

Keyword Arguments in Python

Keyword Arguments in Python

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

Popular posts from this blog

How to adopt Embeddings for Categorical features in Tabular Data using PyTorch's nn.Embedding( )-- Part 2

In the previous post , we set up the context to utilize embeddings for categorical features. In this post, we will figure out how to create these embeddings and combine them with other continuous features to build a neural network model. Dataset Download We will utilize the UCI machine learning repo which has a dataset on credit card default for customers in Taiwan. This dataset is also available in Kaggle . Metadata about this dataset is available on the respective websites. To follow this post, it is recommended to download the dataset from Kaggle. Most of the features are self explanatory. Embedding Creation A few definitions first. Levels in a categorical feature represent unique values available for that categorical feature. For e.g. MARRIAGE has levels 0,1,2,3. Each level of a categorical feature is represented by a vector of numbers. So, if you stack up all the levels together and all the vectors together, you can imagine levels to be a colum

How to adopt Embeddings for Categorical features in Tabular Data using PyTorch's nn.Embedding( )-- Part 1

How to adopt Embeddings for Categorical features in Tabular Data using PyTorch's nn.Embedding( )-- Part 1 In this post, we will talk about using embeddings for categorical features using PyTorch. This post will be broken down into following parts. Dataset Download Data Understanding Data Preprocessing Embedding Creation Define Dataset and Dataloaders in PyTorch Neural Network definition in PyTorch The Training Loop Model Validation The idea about using Embeddings from Categorical Features was first mooted during a Kaggle contest and a paper was also published on this. In the context of NLP and word embeddings, we represent each word in an n dimesnional vector space. In a similar way, we can represent any categorical feature in an n dimesnional vector space as well. 1. Dataset Download We will utilize the UCI machine learning repo which has a dataset on credit card default for customers in Taiwan. This dataset is also av

Standard Normal Distribution with examples using Python

Standard Normal Distribution with examples In our previous post, we talked about Normal Distribution and its properties . In this post, we extend those ideas and discuss about Standard Normal Distribution in detail. What is a Standard Normal Distribution? A Normal Distribution with mean 0 and standard deviation 1 is called a Standard Normal Distribution . Mathematicallty, it is given as below. Fig 1:Standard Normal Probability Distribution Function For comparison, have a look at the Normal Probability Distribution Function. If you substitute mean as 0 ,standard deviation as 1, you derive the standard normal probability distribution function Fig 2: Normal Probability Distribution Function Need for a standard normal probability distribution function We need to extract probability information about events that we are interested in. For this, first we need to convert any normal random variable