Operator overloading and Dunder method in python
class Employee:
profession = "Networking"
def __init__(self, aname, asalary, arole):
self.name = aname
self.role = arole
self.salary = asalary
def printdetails(self):
return F"Name is {self.name} Salary is {self.salary} and role is {self.role}"
@classmethod
def change_profession(cls, newprofessional):
cls.profession = newprofessional
# def __add__(self, other):
# return other.salary is self.salary
# def __add__(sefl, other): # dunder methods
# return other.salary + sefl.salary
# Str have more prority then repr
# if str are not avalible in the programe so the program execute the repr
# but if the str and repr both are avaliable so definately the program execute the str not repr
def __str__(self):
return F"Name is {self.name} Salary is {self.salary} and role is {self.role}"
def __repr__(self):
return f"Employee('{self.name}','{self.salary}'','{self.role}')"
umair = Employee("Umair", 344, "Instructor")
rehman = Employee("rehman", 344, "Admin")
# print(umair + rehman)
print(repr(rehman))
Comments
Post a Comment