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

情到浓时终转凉″ 提交于 2021-02-05 09:13:47

问题


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.


回答1:


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 .




回答2:


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__...




回答3:


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)


来源:https://stackoverflow.com/questions/58973743/need-help-an-error-typeerror-init-takes-5-positional-arguments-but-6-wer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!