Skip to content

Instantly share code, notes, and snippets.

@pmbrull
Created April 1, 2022 16:17
Show Gist options
  • Save pmbrull/5d45ab4630c3ce3ed652f7c04e17a6e3 to your computer and use it in GitHub Desktop.
Save pmbrull/5d45ab4630c3ce3ed652f7c04e17a6e3 to your computer and use it in GitHub Desktop.
Python truth 4
class Coffee:
def __init__(self, total_amount):
self.total_amount = total_amount
def drink(self, amount):
if amount > self.total_amount:
raise ValueError("Not enough coffee!")
self.total_amount -= amount
def __bool__(self):
return bool(self.total_amount)
cup = Coffee(100)
bool(cup) # True
cup.drink(100)
if not cup:
print("Panic!")
# Panic!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment