Skip to content

Instantly share code, notes, and snippets.

@afrinjamanbd
Last active June 18, 2022 16:00
Show Gist options
  • Save afrinjamanbd/05b362584d9c1ce5ce3ba2e698f7bf07 to your computer and use it in GitHub Desktop.
Save afrinjamanbd/05b362584d9c1ce5ce3ba2e698f7bf07 to your computer and use it in GitHub Desktop.
Calculator with Method chaining in Python
class Calculator():
def __init__(self, variable):
self.variable = variable
def add(self, adition):
self.variable = self.variable + adition
print(self.variable)
print ("Calculator.add() function called")
return self
def sub(self, subtraction):
self.variable = self.variable - subtraction
print(self.variable)
print ("Calculator.sub() function called")
return self
def mul(self, multiplication):
self.variable = self.variable * multiplication
print(self.variable)
print ("Calculator.mul() function called")
return self
def div(self, division):
if division != 0:
self.variable = self.variable / division
print(self.variable)
print ("Calculator.div() function called")
return self
else:
print ("Calculator.div() function called")
print ("Math Error")
return self
a = 10
calc = Calculator(a)
calc = calc.add(6).sub(10).mul(4).div(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment