May-05-2020, 04:01 PM
Overwriting is not the right term in this case. Your geometry class inherits from two classes that both have a "surface" method. When calling geometry.surface() which method is used? In your case it uses sphere.surface() because Sphere appears first in
class geometry(sphere,cube):. The geometry.mro() would show the order.
I don't think you want geometry to be a subclass of sphere and cube. Instead I think you want it to be their superclass. Inheritence should be from the generic to the specific. The superclass of sphere should be more generic than a sphere.
class geometry(sphere,cube):. The geometry.mro() would show the order.
I don't think you want geometry to be a subclass of sphere and cube. Instead I think you want it to be their superclass. Inheritence should be from the generic to the specific. The superclass of sphere should be more generic than a sphere.
import math class geometry: """base class for geometetric shapes""" def surface(self): """Return surface area""" raise NotImplementedError('Subclass responsibility') class sphere(geometry): def __init__(self, radius): self.radius = radius def surface(self): return 4 * math.pi * self.radius * self.radius class cube(geometry): def __init__(self, edge): self.edge = edge def surface(self): return 6 * self.edge * self.edge shapes = [sphere(3), cube(3), cube(5), sphere(6)] for shape in shapes: print(shape.surface())