[Tutor] Multiple inheritance - Need clarity

Manprit Singh manpritsinghece at gmail.com
Fri Nov 26 20:51:48 EST 2021


Dear sir,

Below given is a toy example , that calculates the surface area of a
cylinder (area of circles on both sides + area of cylinder wall)

import math
class Circle:

    def __init__(self, radius):
        self.radius = radius

    def circle_area(self):
        return math.pi * self.radius**2

class Rectangle:

    def __init__(self, s1, s2):
        self.side1 = s1
        self.side2 = s2

    def rectangle_area(self):
        return self.side1 * self.side2

class Cylinder(Circle, Rectangle):

    def __init__(self, rad, ht):
        Circle.__init__(self, rad)
        Rectangle.__init__(self, 2*math.pi*rad, ht)

    def cylinder_surface_area(self):
        return 2 * self.circle_area() + self.rectangle_area()

cyl = Cylinder(3, 10)
cyl.cylinder_surface_area()

Gives the answer =

245.04422698000386

Just need to know that the way i have called __init__ of circle & rectangle

inside the init of Cylinder ok or not ? the way i have used circle_area &

rectangle_area inside cylinder_surface_area  is correct or not ?

Kindly guide to do this problem in a better way

Regards

Manprit Singh


More information about the Tutor mailing list