Advertisement
go6odn28

helper_methods_for_class

Feb 24th, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. class Math:
  2.     def __init__(self, a, b, operator):
  3.         self.a = a
  4.         self.b = b
  5.         self.operator = operator
  6.  
  7.     def make_calculations(self):
  8.         if self.operator == "+":
  9.             return self._add(self.a, self.b)
  10.         elif self.operator == "-":
  11.             return self._subtract(self.a, self.b)
  12.  
  13.     # Helper methods for the class
  14.     def _add(self, x, y):
  15.         return x + y
  16.  
  17.     def _subtract(self, x, y):
  18.         return x - y
  19.  
  20.  
  21. obj = Math(10, 5, "+")
  22. print(obj.make_calculations())
  23.  
  24. obj = Math(10, 5, "-")
  25. print(obj.make_calculations())
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement