Operator Overloading And Dunder Methods
# Main Code
class Employee:
no_of_leaves = 89
def __init__(self, aname, asalary, arole):
self.name = aname
self.salary = asalary
self.role = arole
def printdetails(self):
return f"The Name is {self.name}. Salary is {self.salary} and role is {self.role}"
@classmethod
def change_leaves(cls, newleaves):
cls.no_of_leaves = newleaves
@classmethod
def from_str(cls, string):
return cls(*string.split("-"))
# harry = Employee("Harry", 255, "Instructor")
# rohan = Employee("Rohan", 85, "Cleaner")
def __add__(self, other):
return self.salary + other.salary
def __truediv__(self, other):
return self.salary / other.salary
def __repr__(self):
return f"Employee('{self.name}', {self.salary}, '{self.role}')"
# def __str__(se Name is {self.name}. Salary is {self.salary} and role is {self.role}"
emp1 =Employee("Harry", 345, "Programmer")
# emp2 =Employee("Rohan", 55, "Cleaner")
print(str(emp1))
Comments
Post a Comment