Loading…
__str__ defines how an object is displayed when printed:class Product:
def __init__(self, name, price):
self.name = name
self.price = price
def __str__(self):
return f"{self.name}: $" + f"{self.price}"p = Product("Apple", 0.50)
print(p) # Apple: $0.50
Add a __str__ method to Expense that returns:
"[description]: $[amount]"
Example: "Lunch: $15.00"
Downloading Python… (one-time, ~10 MB)
This runs entirely in your browser and is cached for next time.