Give an example of Composition in Python
In composition one of the classes is composed of one or more instance of other classes. In other words one class is container and other class is content and if you delete the container object then all of its contents objects are also deleted.
class Salary: def __init__(self, pay): self.pay = pay def get_total(self): return (self.pay*12) class Employee: def __init__(self, pay, bonus): self.pay = pay self.bonus = bonus self.obj_salary = Salary(self.pay) def annual_salary(self): return "Total: " + str(self.obj_salary.get_total() + self.bonus) obj_emp = Employee(600, 500) print(obj_emp.annual_salary())
Sample output of above program.
Total: 7700
2019-07-07T09:46:15+05:30
2019-07-07T09:46:15+05:30
Amit Arora
Amit Arora
Python Programming Tutorial
Python
Practical Solution