2

I have the following code.py file:

class Shape:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def move(self, delta_x, delta_y):
        self.x += delta_x
        self.y += delta_y

class Square(Shape):
    def __init__(self, side=1, x=0, y=0):
        super().__init__(x, y)
        self.side = side

class Circle(Shape):
    def __init__(self, rad=1, x=0, y=0):
        super().__init__(x, y)
        self.radius = rad

I'm running the code in the Python interpreter like this:

>>> import code
>>> c = code.Circle(1)

I'm getting this error:

Traceback (most recent call last):<br>
...<br>
File "code.py", line 18, in __init__<br>
super().__init__(x, y)<br>
TypeError: super() takes at least 1 argument (0 given)<br>

I don't understand why I'm getting this error. I'm specifying a rad value of 1 and I would assume that since I didn't specify x and y values, Circle should be using the default values of x=0 and y=0 and passing them to Shape via the super() function. What am I missing?

BTW, I'm using Python 2.7.1.

Thanks.

avasal
  • 14,350
  • 4
  • 31
  • 47
Jim
  • 13,430
  • 26
  • 104
  • 155

4 Answers4

7

super requires an argument and this is exactly what the error message is saying. In your case you need to use super(Circle, self) and super(Square, self).

For the gory details you can see this SO question or you can just check the official documentation.

Note that unless you want to do funny things the code can be simplified in

Shape.__init__(self, x, y)

in both cases. Until you understand super and why it can be useful I would suggest to simply stay away from it. You can live an happy life as a productive Python programmer without touching that.

Community
  • 1
  • 1
6502
  • 112,025
  • 15
  • 165
  • 265
  • 1
    Thanks 6502. It's been a long day and I appreciate your non-condescending answer. – Jim Sep 25 '12 at 05:52
1

Use super(Shape, self) instead, you can help(super) in python.

Marcus
  • 6,701
  • 4
  • 19
  • 28
1

Here's some code that does what you need, you also need to be using the "new style class" meaning the base type needs to inherit from object:

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

    def move(self, delta_x, delta_y):
        self.x += delta_x
        self.y += delta_y

class Square(Shape):
    def __init__(self, side=1, x=0, y=0):
        super().__init__(x, y)
        self.side = side

class Circle(Shape):
    def __init__(self, rad=1, x=0, y=0):
        super(Circle, self).__init__(x, y)
        self.radius = rad

P.S. I only fixed Circle and left Square for you to fix.

CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
  • I fixed his Circle code and left Square as an exercise for him. I don't think that merits a minus vote. – CrazyCasta Sep 25 '12 at 06:01
  • OK.. I didn't understand.. But I didn't down-voted your answer.. I generally don't do that unless an answer is far away from the topic.. But, if you wanted him to do some task on that code, you should have written there.. May be someone else down-voted, taking it as wrong answer.. – Rohit Jain Sep 25 '12 at 06:02
1

Finally fixed it. :D Searching through python docs and old Stackoverflow posts for the win.

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

    def move(self, delta_x, delta_y):
        self.x += delta_x
        self.y += delta_y

class Square(Shape):
    def __init__(self, side=1, x=0, y=0):
        super(Square,self).__init__(x, y)
        self.side = side

class Circle(Shape):
    def __init__(self, rad=1, x=0, y=0):
        super(Circle,self).__init__(x, y)
        self.radius = rad

c = Circle(5)

This works. You need to use new style classes by making the top parent (Shape) inherit from object.

References: http://docs.python.org/reference/datamodel.html#newstyle Chain-calling parent constructors in python

Community
  • 1
  • 1
Mitch
  • 53
  • 3