Skip to content

Instantly share code, notes, and snippets.

@Shihab-Shahriar
Created January 15, 2021 20:46
Show Gist options
  • Save Shihab-Shahriar/4a3194b518e3722e7fbb79cd4c19d1f2 to your computer and use it in GitHub Desktop.
Save Shihab-Shahriar/4a3194b518e3722e7fbb79cd4c19d1f2 to your computer and use it in GitHub Desktop.
Simple chaining pattern demonstration in Python
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