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.

5
  • Look carefully at the error message. super() takes at least 1 argument. This doesn't have anything to do with your x and y values. Commented Sep 25, 2012 at 5:42
  • docs.python.org/library/functions.html#super Commented Sep 25, 2012 at 5:43
  • Also, make sure that Shape inherits from object. Commented Sep 25, 2012 at 5:44
  • 1
    Looks like this was written for Python 3. In Python 2.7, super() requires arguments. Commented Sep 25, 2012 at 5:44
  • I'm sitting alone in front of my computer trying to learn Python. I suspect this is a version 2.x vs. 3.0 problem. I'm sorry I'm not as bright as the rest of you guys. Commented Sep 25, 2012 at 5:47

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks 6502. It's been a long day and I appreciate your non-condescending answer.
1

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

Comments

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

Comments

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.

2 Comments

I fixed his Circle code and left Square as an exercise for him. I don't think that merits a minus vote.
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..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.