添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

super().__init__(place, colour, x, y) rather than super().__init__(self, place, colour, x, y) – Rabbid76 Nov 21, 2019 at 11:25

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

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)
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.