Python Tutorial for Beginners 30 - Python Composition

 


hey guys welcome to the next blog on Python tutorial for beginners in this blog I'm going to show you what is composition and how to use class composition in Python so to start with I have two classes here one is an employee class and other is the salary class now in the salary class I have this init method which takes two argument P and bonus and it sets these two arguments to the member variables of this class also I have a method called annual salary which is used to calculate the salary now in the employee class I have this constructor which takes four arguments which is name age P and bonus now what do you think can we apply inheritance here the answer is no because there is no is our relationship between salary and employee so employee is not a salary and salary is not an employee but somehow I want to delegate the responsibility of calculating the employee salary to the salary class because calculating salary is in itself is a big task I have just simplified this class in the form of just calculating the annual salary but the employee can have his own taxes or he has taken some leaves or many factors affects the salary of an employee so this salary calculation let's say we want to delegate to the salary class but whose salary is this this salary is the employees salary so we know that we cannot use inheritance here but we can use a concept called composition here and the composition means that we are just delegating some responsibilities from one class to the another class so how to delegate this responsibility we can just create one more variable here for example self dot obj underscore salary which is the salary object we going to create using the salary class okay and this salary class takes two parameters one is pay and other is bonus which we are already providing using the employee init method so I'm going to just pass this pay comma the bonus into the salary class so we are instantiating the salary class inside the employee class so here one class which is an employee class acts like a container of the other class which acts like a Content okay so salary class is the content and the employee class is the container of this salary class now in order to calculate the total salary we can define one more method here and I'm going to just say total underscore celery and then what we are going to do is we are going to call our object salary member variable because this gives us the object of the salary so I'm going to just call object salary and we can call this method from the salary class which gives us the annual salary and now I'm going to just create the object of this employee class so let me just write EMP is equal to employee which takes four parameter first is a name second is the age third is the pace so let's say P is 15,000 and the bonus is 10,000 and now I just want to print the total salary of this employee so I can just write print and then EMP object dot the total salary which is total salary method and you can see here this total salary is returning let me just use the return keyword also because we are returning this annual salary from the salary class using this total salary method so let's run the code once again and let's see what happens and you can see annual salary is printed and this annual salary is kal cállate 'add using the salary class so what this employee class has done it has delegated some responsibilities to the other class which is the salary class which gives us the annual salary of an employee and this here is called composition now one interesting thing which you will notice here is we are not instantiating salary here we are just instantiate in the salary inside an employee class so as soon as you delete an instance of an employee the salary class instance will be automatically deleted so whenever you want to delegate some responsibilities of one class to the another class it's called composition one more example of composition can be a book class and a chapter class so a chapter is not a book and book is not a chapter but we can delegate some responsibilities from a book class to the chapter class so this is how you can use composition in Python I hope you have learned something new this time and I will see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News