class Item:
"""Any piece of rental equipment."""
def __init__(self, item_id, daily_cents):
self.item_id = item_id
self.daily_cents = daily_cents
self.available = True
def cost(self, days):
"""Return the rental cost in cents."""
return self.daily_cents * days
def label(self):
return f"{self.item_id} ({type(self).__name__})"
class Skis(Item):
"""Skis: an Item that also has a length."""
def __init__(self, item_id, daily_cents, length_cm):
super().__init__(item_id, daily_cents) # Item sets up the shared part
self.length_cm = length_cm
def suits(self, height_cm):
"""This shop's rule of thumb: skis from height - 20 cm up to height."""
return height_cm - 20 <= self.length_cm <= height_cm
class Helmet(Item):
"""A helmet: an Item with a size that must be cleaned after use."""
def __init__(self, item_id, daily_cents, size):
super().__init__(item_id, daily_cents)
self.size = size
self.sanitized = True
def mark_used(self):
self.sanitized = False
s = Skis("S1", 3500, 160)
h = Helmet("H1", 800, "M")
print(s.label(), s.cost(3), s.suits(172))
print(h.label(), h.cost(3), h.sanitized)
h.mark_used()
print(h.sanitized, isinstance(h, Item), isinstance(h, Skis))
print([c.__name__ for c in Skis.__mro__])
equipment.py: one superclass and two subclasses.