Inheritance: new method for Shape doesn't work


#1

Hello!

I'm experimenting with the code in this section to better understand the concept of inheritance.

I just created a new method called display_sides() in Class Shape. However when i ran the code it doesn't totally work. here is the output, in bold:

test_print
3
None
a
b
c

test_print
Traceback (most recent call last):
** File "python", line 24, in **
** File "python", line 7, in display_sides**
AttributeError: 'Triangle' object has no attribute 'number_of_sides'

I had expected the final line of the output to be 3. but instead, this just threw an error ... which doesn't make sense (it's not clear to me why Triangle didn't inherit number_of_sides from Shape)

below is the code. any suggestions? what am i missing?

class Shape(object):
    """Makes shapes!"""
    def __init__(self, number_of_sides):
        self.number_of_sides = number_of_sides
    def display_sides(self):
        print 'test_print'
        print self.number_of_sides

class Triangle(Shape):
    def __init__(self, side1, side2, side3):
        self.side1 = side1
        self.side2 = side2
        self.side3 = side3
        
my_shape = Shape(3)
my_triangle = Triangle('a', 'b', 'c')

print my_shape.display_sides()

print my_triangle.side1
print my_triangle.side2
print my_triangle.side3
print
print my_triangle.display_sides()


#2

update: i've solved the problem by adding self.number_of_sides as an attribute to the Triangle class.
below is the new code, which now works.

so in this example, it appears class Triangle inherited the methods of Shape - but not all of the attributes. is that a correct inference? if so, this leaves me a little confused given that codeacademy defined Inheritance as "the process by which one class takes on the attributes and methods of another"
but that didn't seem to happen in this example (only the methods were inherited)

class Triangle(Shape):
    def __init__(self, number_of_sides, side1, side2, side3):
        self.number_of_sides = number_of_sides
        self.side1 = side1
        self.side2 = side2
        self.side3 = side3

#3

It is refreshing to see effort. Good work, @justin789!

In order to inherit the attribute properly, you can call the __init__ method of the superclass (parent) in the class's __init__.

Here is an example showing just attributes to save space.

class Shape(object):
  def __init__(self, number_of_sides):
    self.number_of_sides = number_of_sides

class Triangle(Shape):
  def __init__(self, side1, side2, side3):
    Shape.__init__(self, 3)
    self.side1 = side1
    self.side2 = side2
    self.side3 = side3

Do you see how by explicitly calling Shape's __init__ method passing in 3 for number_of_sides defines a Triangle in a "final" sense rather than accepting a number_of_sides argument in Triangle's __init__ method which causes a redundancy?


#4

interesting - thanks @jacobsandersen!
below is the full code with your suggestion. it certainly works and it does eliminate the redundancy that was present in my fix above.
appreciate your help!
cheers
justin

class Shape(object):
    """Makes shapes!"""
    def __init__(self, number_of_sides):
        self.number_of_sides = number_of_sides
        
    def display_sides(self):
        print "number of sides: %s" % self.number_of_sides

class Triangle(Shape):
    def __init__(self, side1, side2, side3):
        Shape.__init__(self, 3)        
        self.side1 = side1
        self.side2 = side2
        self.side3 = side3
        
some_shape = Shape(5)
my_triangle = Triangle('a', 'b', 'c')

print some_shape.display_sides()
print

print my_triangle.side1
print my_triangle.side2
print my_triangle.side3
print
print my_triangle.display_sides()

output:

number of sides: 5
None

a
b
c

number of sides: 3
None


#5

You are making headway, here. although there are some questions to resolve. We know that a triangle is strictly defined. For instance, the sum of its angles may not exceed 180. The sum of the shorter sides may not be less than the length of the longest side.

So in the above we are left with guessing what three string values contribute, other than being names of the sides. Only problem is we already gave them names. Would it were the author had given the names to which we've become accustomed... a, b, c.

self.a = a
self.b = b
self.c = c

Now we're leaning toward your intuition.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.