# # Example of multiple inheritance -- # basic animal classes, house pet, cats with error checking # # Matt Bishop, MHI 289I, Fall 2024 # # # base class: animal # class Animal: # expected to be one of these kindofanimal = [ "lion", "bear", "dog", "giraffe", "cat" ] def __init__(self, kind): # if it's in the list, OK -- otherwise disallow it if kind in Animal.kindofanimal: self.kind = kind else: self.kind = None def __str__(self): # if it's not in the list, say so if self.kind: return "Your animal is a " + self.kind else: return "Your animal is of an unknown type" # # another base class: pets # class Pets: # expected to be one of these kindofpet = [ "indoor", "outdoor", "in and out" ] def __init__(self, kind): # if it's in the list, OK -- otherwise disallow it if kind in Pets.kindofpet: self.housetype = kind else: self.housetype = None def __str__(self): # if it's not in the list, say so if self.housetype: return "Your pet is " + self.housetype else: return "Your pet is of an unknown type" # # now the subclass, as cat is an animal # class Cat(Animal, Pets): # breeds of cats catbreed = [ "Persian", "Manx", "Siamese", "domestic" ] def __init__(self, house, breed): # instatiate as an animal Animal.__init__(self, "cat") # indoor or outoor pet? Pets.__init__(self, house) # add in the new attribute if breed in Cat.catbreed: self.breed = breed else: self.breed = None def __str__(self): # if it's not in the list, say so if self.breed: return Animal.__str__(self) + "; " + Pets.__str__(self) + "; breed is "+self.breed else: return Animal.__str__(self) + "; " + Pets.__str__(self) + "; breed is unknown" # # nw the example usage # # first, the class -- show a lion a = Animal("lion") print(a) # now, the subclass, with a valid breed c = Cat("in and out", "Persian") print(c) # and the subclass with an invalid breed c2 = Cat("outdoor", "Himalayan") print(c2)