Inheritance Syntax--Why doesn't Triangle class inherit number_of_sides?


#1

Why does the Triangle class not inherit the number_of_sides function from the Shape class? Or what syntax would I use to call inherit the number_of_sides attribute from Shape? The code below results in an error because Triangle doesn't have number of sides defined.

Code Snippet

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

 class Triangle(Shape):
     '''Makes triangles'''
     def __init__(self,side1,side2,side3):
         self.side1=side1
         self.side2=side2
         self.side3=side3
 a=Shape(4)
 print a.number_of_sides
 a=Triangle(3,4,5)
 print a.side1
 print a.number_of_sides

Results in an error

4
3
Traceback (most recent call last):
  File "python", line 19, in <module>
AttributeError: 'Triangle' object has no attribute 'number_of_sides'`

#2

@mimi314159: Similar if not equal to your question.
"Triangle overrides init so you get the Triangle's version instead of the one in Shape" - @ionatan


#3

That makes sense. Thanks.


#4

Not only me but @ionatan as well :slightly_smiling: