Skip to content Skip to sidebar Skip to footer

Python, Inheritance, Super() Method

I'm new to python, I have the code below which I just can't get to work:- This is inheritance, I have a circle base class and I inherit this within a circle class (just single inhe

Solution 1:

You need to pass in the Circle class instead:

text = super(Circle, self).ToString() + "{radius = " + str(self.radius) + "}\n"

super() will look through the base classes of the first argument to find the next ToString() method, and Point doesn't have a parent with that method.

With that change, the output is:

>>> print( shapeTwo.ToString() )
{x:0.0, y:0.0}
{radius = 12}

Note that you make the same mistake in your __init__; you are not calling the inherited __init__ at all. This works:

def__init__(self, x, y, radius):
    super(Circle, self).__init__(x ,y)
    self.radius = radius
    print("circle constructor")

and then the output becomes:

>>>shapeTwo = Circle(4, 6, 12)
point constructor
circle constructor
>>>print( shapeTwo.ToString() )
{x:4, y:6}
{radius = 12}

Post a Comment for "Python, Inheritance, Super() Method"