Notice that Facility, Warehouse, and ShippingCenter have no methods in common. This is a strong indication that they are not subclasses but traits or the watered-down mixin. A mixin is just a bundle of methods which gets injected into a class.
Mixin methods can be overridden, and mixins can have abstract methods which the mixee is required to implement. All of that is illustrated below.
class HasFacility: def set_address(self, address): self._address = address def address(self): return self._address def set_facility_id(self, facility_id): self._facility_id = facility_id def facility_id(self): return self._facility_idclass HasWarehouse: def store_item(self, item): print(f"I will store {item}") def retrieve_item(self, item): print(f"I will retrieve {item}")class HasShippingCenter: def shipper(self): raise NotImplementedError def ship_item(self, item, customer): print(f"I will ship {item} to {customer} using {self.shipper()}")class HybridFacility(HasFacility, HasWarehouse, HasShippingCenter): def shipper(self): return "PostHaste" def retrieve_item(self, item): print(f"I will FETCH the {item}")hf = HybridFacility()hf.set_address("123 Four Five Road")print(hf.address())hf.store_item("that thing")hf.retrieve_item("that thing")hf.ship_item("that thing", "you")
Note that I avoided making use of constructors in the mixins because none of them can claim to own the constructor. Instead, one sets attributes after the object is created using accessor methods to avoid quietly colliding on object attributes.
If you're writing a method to store something, rather than requiring a class, all you need to require is that the object HasWarehouse. For example, we can implement a new "warehouse" with no facility called a Bag.
class Bag(HasWarehouse): passbag = Bag()bag.store_item("sugar packets")bag.retrieve_item("sugar packets")
Both Bag and HybridFacility can be used interchangeably anywhere you need an object which HasWarehouse.
I'm not a great Python programmer, so they may have a better way to do it, but that's the general idea. Check out the traits library for a more complete implementation.