Class & dataclass: My Wholesome Guide to Learning Python (Part 3)

Nepal Brothers
2 min readSep 28, 2021

If you read any of the previous articles, those are really awesome solid core python concepts that you now have in your pocket. In this part, we will cover some interesting concepts such as class, data class. They are object oriented concepts and are really handy as your project becomes complex.

Previously, we have just been talking about the salary about a single employee, Scooby Doo. Now, lets build on this and get the salary of the whole Mystery Inc.

Class: Lets have a Mystery Inc and lets add its employees

Data class: Lets add more information on the Employee

Does it feel like an employee needs to be paid. And, what about firstname and lastname. It sort of feels like we need a class that will hold all these information properly. Lets do that now.

Can you tell how we would go about getting a total salary of Mystery Inc in a month?

class MysteryInc:
# ignored for berevity

def get_monthly_salary(self) -> int:
total_salary = 0
for employee in self.employees:
total_salary += employee.monthly_salary
return total_salary




# creating an instance of the class.
print("All Employees salaries are ", ourMysteryInc.get_monthly_salary())

Townsville: Describing employees of the city

Mystery Inc is one of the companies, and your own startup can be another company. If you think of the structure, it is going to be almost similar to the Mystery Inc right? So, lets refactor the code so that they both can be created in a similar way, or any company actually if we wanted!

This is really good stuff. Now, we will head to actually reading through a yaml file that has all the information about employee and company. We will then construct the class based on it. See you in part 4.

--

--