Created
April 1, 2022 16:17
-
-
Save pmbrull/5d45ab4630c3ce3ed652f7c04e17a6e3 to your computer and use it in GitHub Desktop.
Python truth 4
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 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