Riju21

37_magic_method

May 12th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1.  
  2. class Employe:
  3.  
  4.     raise_amount = 2.5
  5.  
  6.     def __init__(self, fname, lname, pay):
  7.         self.first = fname
  8.         self.last = lname
  9.         self.pay = pay
  10.  
  11.     def name(self):
  12.         self.mail = self.first + '@gmail.com'
  13.         return ('{}'.format(self.mail))
  14.  
  15.     def apply(self):      
  16.         self.total = float(self.pay) * self.raise_amount
  17.         return ('{} '.format(self.total))
  18.  
  19.     def __repr__(self):     # magic method
  20.         return ('Employee: {} , {} , {} '.format(self.first, self.last, self.pay))
  21.  
  22.     def __str__(self):      # magic method
  23.         return ('{} - {} - {} '.format(self.name(), self.apply(), self.first))
  24.          
  25.     def __add__(self, other):           # problem
  26.         return self.pay + other.pay
  27.  
  28.     # def __len__(self):       # magic method
  29.     #     return len(self.name())
  30.        
  31.  
  32. emp_1 = Employe('riju', 'chowdhury', 3000)
  33. emp_2 = Employe('alen', 'mok', 4000)
  34.  
  35. print(emp_1.apply())
  36. print(emp_1 + emp_2)
  37. # print(len(emp_1))
  38. print(len(emp_1.name()))
  39. # print(int.__add__(1, 2))
  40.  
  41. print(repr(emp_1))  # magic_method: repr()
  42. print(str(emp_1))   # magic_method: str()
Advertisement
Add Comment
Please, Sign In to add comment