3

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 inheritance here).

I understand the issue is within the ToString() function within the circle class, specifically the line, text = super(Point, self).ToString() +.. which requires at least a single argument, yet I get this:

AttributeError: 'super' object has no attribute 'ToString'

I know super has no ToString attribute, but the Point class does -

My code:

class Point(object):
    x = 0.0
    y = 0.0

    # point class constructor
    def __init__(self, x, y):
        self.x = x
        self.y = y
        print("point constructor")

    def ToString(self):
        text = "{x:" + str(self.x) + ", y:" + str(self.y) + "}\n"
        return text

class Circle(Point):
    radius = 0.0

    # circle class constructor
    def __init__(self, x, y, radius):
        super(Point, self)              #super().__init__(x,y)
        self.radius = radius
        print("circle constructor")

    def ToString(self):
        text = super(Point, self).ToString() + "{radius = " + str(self.radius) + "}\n"
        return text


shapeOne = Point(10,10)
print( shapeOne.ToString() ) # this works fine

shapeTwo = Circle(4, 6, 12)
print( shapeTwo.ToString() ) # does not work
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
user1937226
  • 41
  • 1
  • 2

1 Answers1

5

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}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • many thanks(!) that's a step further but not the desired result, so I think I've done something else wrong here. I now get this - point constructor {x:10, y:10} circle constructor {x:0.0, y:0.0} {radius = 12} – user1937226 Dec 29 '12 at 23:33
  • when my circle object was, shapeTwo = Circle(4, 6, 12) – user1937226 Dec 29 '12 at 23:36
  • So I should of asked, how do I pass the x,y points from Circle to Point? – user1937226 Dec 29 '12 at 23:38
  • OK, thanks, I now understand - so to be clear, to use the inherited initialization I have to pass the current class to super(), as well as the required arguments? as in, super(inheritedClass, self).__init__(base class arguments) ? – user1937226 Dec 29 '12 at 23:45