Darcey Mckelvey Darcey Mckelvey - 13 days ago 5x
Python Question

OOP : Trying to design a good class structure

This is homework so off the bat I am not asking to do it for me but to clarify for me so that I understand.

Here is what the assignment said about the program I have to write:

Write an OO Python Program showing how your sub-classes inherit from the super-class(es) You can have one class (preferred but not a must) or two or more super classes.

What I have to do is write a oo python program showing sub-classing inherited by a class or two or more superclasses. The program has to be about shapes. As an example of what I mean by that is:

Shape: Square


  • length

  • width


  • area

  • perimeter

I have more shapes of course but from that I find the common attributes and methods from all the shapes and make superclasses and sup-classes.

My super classes are like: 2dShapes, circles and 3dShapes. My sub-classes are like length and width. My methods are area and perimeter. Note I am rambling at this point. The code snippet below does not show this instead I was thinking about making a superclass for attributes and methods and than sub-classes for the shapes? maybe?

Question: is this a good class structure? Is there a better way to structure the classes here? Here is an example of what I'm thinking about how to do this.

class Shapes(attributes):
def __init__(self):
# not sure how to go on to make the attributes like height and length, radius ect. Maybe like this???
def height(self):
raise NotImplementedError # again this program is not supose to actually do anything. The program is just for us to understand inheritance with classes and super classes.

class 2dShapes(Shapes):
class Square(self):
def height(self):
# ???

So at this point I am so confused about where to start. Also I am super new to python so be gentle to me :p


One of the big ideas of using inheritance is to be able to re-use code.. If you have a large body of functions that are the same for several classes, having one parent class that holds those functions allows you to only write them once, and to modify them all at the same time. for example, if you want to implement classes for the shapes: Square, Rectangle, and Parallelogram, you could create a parent class: Quadrangle that contains things like a generic area or perimeter functions:

class quadrangle(object):
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return 2*self.length + 2*self.length

class square(quadrangle):
    def __init__(self, length):
        super(square, self).__init__(length, length) #creates a quadrangle with equal lenght and width

class rectangle(quadrangle): #renamed for convienience sake but otherwise the same

class parallelogram(quadrangle):
    def __init__(self, length, width, angle): #angle in radians
        self.angle = angle
        super(parallelogram, self).__init__(length, width)

    def perimeter(self): #override parent's perimiter
        #do some math
        return 5