Last active
June 18, 2022 15:35
-
-
Save afrinjamanbd/46c2deb49a9a44228eacb74a838297dc to your computer and use it in GitHub Desktop.
Method Chaining example in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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