Created
January 15, 2021 20:46
-
-
Save Shihab-Shahriar/4a3194b518e3722e7fbb79cd4c19d1f2 to your computer and use it in GitHub Desktop.
Simple chaining pattern demonstration 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 Balance: | |
def __init__(self): | |
self.bal = 0 | |
def add(self, x): | |
self.bal += x | |
return self | |
def deduct(self, x): | |
self.bal -= x | |
return self | |
b = Balance() | |
b.add(7).deduct(2).add(5) | |
print(b.bal) # Outputs 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment