class Vehicle:
"""Any vehicle in the transit fleet."""
def __init__(self, ident, capacity):
self.ident = ident
self.capacity = capacity # passengers
class Bus(Vehicle):
"""A bus that runs on a numbered route."""
def __init__(self, ident, capacity, route):
super().__init__(ident, capacity)
self.route = route
class Train(Vehicle):
"""A train made of several cars."""
def __init__(self, ident, capacity, cars):
super().__init__(ident, capacity)
self.cars = cars
class Bike(Vehicle):
"""A share bike parked at a dock."""
def __init__(self, ident, dock):
super().__init__(ident, 1)
self.dock = dock
fleet = [Bus("B-101", 70, 44), Train("T-7", 1100, 6), Bike("K-12", "Main St"), Bus("B-205", 55, 9)]
print(sum(v.capacity for v in fleet)) # works for every Vehicle
for v in fleet:
if isinstance(v, Bus): # safe "downcast"
print(v.ident, "route", v.route)
print(isinstance(fleet[1], Vehicle), type(fleet[1]) is Vehicle)
print(issubclass(Bike, Vehicle), issubclass(Vehicle, Bike))