本文介绍了需要帮助时出现错误"TypeError:__init __()接受5个位置参数,但给出了6个位置参数".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我才刚刚开始上课并了解基本概念,但是我不明白为什么它会给我这个错误.

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

我的代码:

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

我收到的错误:


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.

推荐答案

>code> super() 返回一个代理对象,该代理对象将方法调用委托给父对象.
因此必须是:

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

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

该调用就像调用实例方法一样.

The call is like calling an instance method .

这篇关于需要帮助时出现错误"TypeError:__init __()接受5个位置参数,但给出了6个位置参数".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-27 07:25