Eugene Levinson
Eugene Levinson

Reputation: 182

Need help an error "TypeError: __init__() takes 5 positional arguments but 6 were given"

I am just started getting into classes and understand the basic concept however I don't understand why it gives me this error.

My code:

import pygame

pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False

while not done:

        for event in pygame.event.get():

                if event.type == pygame.QUIT:
                        done = True

        class shape():
            def __init__(self, place, colour, x, y):
                self.place = place
                self.colour = colour
                self.x = x
                self.y = y

        class rectangle(shape):
            def __init__(self, place, colour, x, y, length, width):
                super().__init__(self, place, colour, x, y)

                self.length = length
                self.width = width

                pygame.draw.rect(screen, colour, pygame.Rect(x, y, lenght, width))





        Rectangle = rectangle(screen, (0, 128, 255), 30, 30, 60, 60)

        pygame.display.flip()

The error I receive:


Traceback (most recent call last):
  File "Pygame.py", line 34, in <module>
    Rectangle = rectangle(screen, (0, 128, 255), 30, 30, 60, 60)
  File "Pygame.py", line 23, in __init__
    super().__init__(self, place, colour, x, y)
TypeError: __init__() takes 5 positional arguments but 6 were given

I am not sure why it gives an error as I am creating a "rectangle" object. I found some examples and they seemed to be the same as mine.

Upvotes: 1

Views: 9980

Answers (3)

Macintosh_89
Macintosh_89

Reputation: 708

I faced same issue , iam using python 2.7.x. I resolved it using classname directly. Here is the explination Python extending with - using super() Python 3 vs Python 2
Try this

shape.__init_(self,place, colour, x, y)

Upvotes: 1

Rabbid76
Rabbid76

Reputation: 211268

super() returns a proxy object that delegates method calls to a parent.
So it has to be:

super().__init__(self, place, colour, x, y)

super().__init__(place, colour, x, y)

The call is like calling an instance method .

Upvotes: 0

Antonio Romano
Antonio Romano

Reputation: 285

The issue is in super().__init__(self, place, colour, x, y).

You are trying to call the initializer of a class you extends, but your rectangle class is not extending any class, this means that the super() is looking for something builtin class you don't have control over it.

Since you said you took this from an example, my guess is that you are missing to extend a class.

To make the script working you can remove the call to super().__init__...

Upvotes: 1

Related Questions