In the previous post, we introduced the topic of classes in Python. We will continue to develop these ideas in this post. We need to calculate the newspaper,egg and milk expenses for any person. Using this information, we will finally calculate the total expenses. Let's see this in action. Recall that our __init__ method allowed us to initialize name of the person, the kind of milk, eggs and newspaper puchased by the person.
def __init__(self,name,milk_type,newspaper_type,egg_type):
self.name = name
self.milk_type= milk_type
self.newspaper_type = newspaper_type
self.egg_type = egg_type
To calculate the egg expenses incurred, we define a method called calculate_egg_expenses. Recall that a function defined within a class is called a method. Code block below explains this.
def calculate_egg_expenses(self,number_of_eggs_per_day):
if self.egg_type =="small":
self.egg_expenses = Expenses.small_egg_rate*number_of_eggs_per_day * Expenses.num_days
elif self.egg_type== "large":
self.egg_expenses = Expenses.large_egg_rate * number_of_eggs_per_day * Expenses.num_days
We first check the type of egg which is opted for by the person. Accordingly, egg expenses can be calculated based on egg rate, number of eggs per day purchased and number of days in a month. For ease of explanation, we have assumed number of days in a month as 31.
Notice the self keyword passed to the method. It helps in creating egg_expenses specific to the class instance. Also , we can see that variables num_days, small_egg_rate, large_egg_rate are being accessed using the Expenses class. As explained in the previous post, we call these variables as class variables. However, it turns out, if you use self keyword instead of Expenses, it behaves in a similar way.
Why is that? The reason is, class variables by default become part of the instance of the class. So we can either use class name or self to access these variables. Code block below shows this.
def calculate_egg_expenses(self,number_of_eggs_per_day):
if self.egg_type =="small":
self.egg_expenses = self.small_egg_rate*number_of_eggs_per_day * self.num_days
elif self.egg_type== "large":
self.egg_expenses = self.large_egg_rate * number_of_eggs_per_day * self.num_days
In a similar way, we can calculate newspaper and milk expenses by creating below methods.
def calculate_newspaper_expenses(self):
if self.newspaper_type == "deccan":
self.newspaper_expenses = Expenses.deccan_herald_rate* Expenses.num_days
elif self.newspaper_type == "toi":
self.newspaper_expenses = Expenses.toi_rate* Expenses.num_days
def calculate_milk_expenses(self,litres_of_milk_per_day):
if self.milk_type=="blue":
self.milk_expenses= Expenses.blue_milk_rate* litres_of_milk_per_day * Expenses.num_days
elif self.milk_type == "green":
self.milk_expenses = Expenses.green_milk_rate * litres_of_milk_per_day * Expenses.num_days
We now define a method called total_expenses. It takes as input two parameters, litres_of_milk_per_day and number_of_eggs_per_day. When this method is called, it internally calls calculate_egg_expenses , calculate_milk_expenses and calculate_newspaper_expenses. After completion of each method, results are stored in egg_expenses , milk_expenses and newspaper_expenses . To reiterate, these variables are associated with the instance of the class as expenses can differ for different people. Final step is to calculate expenses which is a sum of the three variables just calculated.
def total_expenses(self,litres_of_milk_per_day, number_of_eggs_per_day):
self.calculate_egg_expenses(number_of_eggs_per_day)
self.calculate_milk_expenses(litres_of_milk_per_day)
self.calculate_newspaper_expenses()
self.expenses = self.egg_expenses+ self.newspaper_expenses+ self.milk_expenses
Finally, we need a method to see what was the expenses incurred under different sections. So, we just create a helper function to display this information.
def display_expenses(self):
print(f"Milk expenses equal Rs {self.milk_expenses}")
print(f"Egg expenses equal Rs {self.egg_expenses}")
print(f"Newspaper expenses equal Rs {self.newspaper_expenses}")
print(f"Total expenses equal Rs {self.expenses}")
This is how we would run a sequence of Python commands to display this information.
Rohit_expenses = Expenses(name = "Rohit Rudrapatna" , milk_type = "green", newspaper_type = "deccan", egg_type = "small")
Rohit_expenses.total_expenses(litres_of_milk_per_day = 2,number_of_eggs_per_day= 2)
Rohit_expenses.display_expenses()
--->Milk expenses equal Rs 1302
Egg expenses equal Rs 496
Newspaper expenses equal Rs 186
Total expenses equal Rs 1984
Hope this post gives you a gentle introduction to classes. In the upcoming posts, we will get into more details about OOPs.
Comments
Post a Comment