OOP : Trying to design a good class structure
<p>Follow up code snippet review question I posted: <a href="http://codereview.stackexchange.com/questions/143576/oop-designing-class-inheritances">http://codereview.stackexchange.com/questions/143576/oop-designing-class-inheritances</a></p>

<p>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.</p>

<p>Here is what the assignment said about the program I have to write:</p>

<p><strong>Required:</strong>
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.</p>

<p>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:</p>

<p>Shape: Square</p>

<p>Attributes:</p>

<ul>
<li>length </li>
<li>width</li>
</ul>

<p>Methods:</p>

<ul>
<li>area</li>
<li>perimeter</li>
</ul>

<p>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.</p>

<p>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?</p>

<p>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.</p>

<pre><code>class Shapes(attributes):
    def __init__(self):
        attributes.__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):
            # ???
</code></pre>

<p>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</p>

<p>Be careful not to confuse "attributes" with "methods". There is an @property syntax which wraps attribute access within a method call, but you should ignore that for now.</p>

<pre><code>class Shapes(attributes):
    def __init__(self): 
</code></pre>

<p>For this part, just absorb the arguments here. e.g:</p>

<pre><code>def __init__(self, *args, **kwargs):
    pass
</code></pre>

<p>Reason being, that you are only using Shapes to pass them to a subclass. <code>*args</code> absorbs lists of named arguments, while <code>**kwargs</code> absorbs dictionaries. So this <code>init()</code> will accept <code>my_shapes_instance = Shapes(length, width, height)</code> because it has <code>*args</code>, and it would accept <code>Shapes(length, width, height, {'cost': '10'})</code> because it has <code>**kwargs</code> as well. </p>

<p>If it were <code>__init__(length, width, height)</code> and you passed <code>(length, width, height, color)</code> then it would not work. But if you use <code>*args</code> then it will accept anything. Will it <em>use</em> all of these arguments? Only if you define that it does.</p>

<p>You can ignore **kwargs for now since you are not initializing these objects with dictionaries.</p>

<pre><code>attributes.__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.
</code></pre>

<p>What you have done above is define a method "height", not an <em>attribute</em> "height". What you want is more like this:</p>

<pre><code>def __init__(self, height):
    self.height = height
</code></pre>

<p>Even better is this, but do it in the Square subclass:</p>

<pre><code>class Square(Shapes):
    def __init__(self, height, *args, **kwargs):
        super(Square, self).__init__(height, *args, **kwargs)
        self.height = height
</code></pre>

<p>Now you can subclass Square with Rectangle, adding in new args as you go.  Just follow a similar <strong>init</strong> pattern as above. Rectangle will not need you to add a height method, since it is already available from the parent.</p>

python python-3.x oop inheritance