Skip to content

Instantly share code, notes, and snippets.

@afrinjamanbd
Last active June 18, 2022 15:35
Show Gist options
  • Save afrinjamanbd/46c2deb49a9a44228eacb74a838297dc to your computer and use it in GitHub Desktop.
Save afrinjamanbd/46c2deb49a9a44228eacb74a838297dc to your computer and use it in GitHub Desktop.
Method Chaining example in python
class Calculator():
def __init__(self, variable):
self.variable = variable
def add(self, adition):
total = self.variable + adition
print(total)
print ("Calculator.add() function called")
self.variable = total
return self
def sub(self, subtraction):
total = self.variable - subtraction
print(total)
print ("Calculator.sub() function called")
self.variable = total
return self
def mul(self, multiplication):
total = self.variable * multiplication
print(total)
print ("Calculator.mul() function called")
self.variable = total
return self
def div(self, division):
total = self.variable / division
print(total)
print ("Calculator.div() function called")
self.variable = total
return self
a = 10
cal = Calculator(a)
cal = cal.add(6).sub(10).mul(2).div(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment