TypeError:\uuuu init\uuuu()缺少1个必需的位置参数:,但我正在提供必需的参数

2024-07-04 16:45:56 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑:问题解决了,我确实是个白痴,这确实是一个非常愚蠢的错误。解决方案是,我在创建Jouer实例时忘记了给出UI实例。。。对不起,谢谢所有帮助我的人

我一直在尝试编写一个国际象棋游戏。我现在正在测试它,我得到了一个“奇怪”的错误(根据我的经验,我可能只是搞砸了,但我的情况并不完全像其他关于这个错误的帖子上的人所看到的那样,我一直在搜索我的代码和文档,寻找线索,几个小时都没有成功:所以我来了)

因此,总结一下代码(我不会把我以前成功运行过的代码放在这里,只展示应该相关的内容):

我有一个board类,它基本上是我的模型和控制器。在构造函数中,我要求两个参和者提供参数,一个UI类和一个哈希类。在我实现最后两个并将其添加到构造函数中之前,代码运行良好

class Plateau:
    def __init__(self, j1, j2, UI, Hash):
        self.UI = UI
        self.UI.ajoutePlateau(self)
        self.Hash = Hash
        self.Hash.hashPlateau(self)
        # some code to deal with the rest
        ...
        #
        self.UI.initAffichage()

然后,我有一个UI接口和一个控制台UI(在我的代码中是UITerminal),它们从中继承。正如预期的那样,向用户显示棋盘的内容,以及询问人类玩家(如果有)想要玩什么

class UI:
    def __init__(self):
        self.Plateau = None

    def ajoutePlateau(self, Plateau):
        self.Plateau = Plateau

    # a bunch of method that throw an exception and are overrided by the child class
        ...
    #
class UITerminal(UI):
    def __init__(self):
        #super(UITerminal, self).__init__(self)
        #super().__init__(self)
        #UI.__init__(self)
        #super(UITerminal, self).__init__()
        super().__init__()
        #UI.__init__()

    # the method that had to be overridden
        ...
    #

我还尝试了UITerminal构造函数的几个版本(见上面的注释)。 或者什么都没有,因为它根本不需要(我想…)

然后是散列,它以与UI相同的方式构建:一个接口,一个子接口

class Hash:
    def __init__(self): 
        pass

    # a bunch of method that throw an exception and are overridden by the child class
        ...
    #

class ZombristHash(Hash):
    def __init__(self):
        #super(ZombristHash, self).__init__(self)
        #super().__init__(self)
        #Hash.__init__(self)
        #super(ZombristHash, self).__init__()
        super().__init__()
        #Hash.__init__()

        # a bunch of code to init the zombristhash
        ...
        #

和UI一样,我尝试了多种方法来调用接口构造函数

然后我有我的main,它只有一行,是抛出错误的地方:

p = Plateau(Humain("j1"), Humain("j2"), UITerminal(), ZombristHash())

错误是:

Traceback (most recent call last): File "plateau.py", line 298, in <module> p = Plateau(Humain("j1"), Humain("j2"), UITerminal(), ZombristHash()) 

TypeError: __init__() missing 1 required positional argument: 'UI'.

据我所知,他告诉我,我没有给董事会构造函数一个UI作为参数,但我这么做了,我不明白发生了什么

我尝试了quamran的建议:

p = Plateau(None, None, None, None)

它似乎认为现在一切都好了

Traceback (most recent call last):
  File "plateau.py", line 298, in <module>
    p = Plateau(None, None, None, None)
  File "plateau.py", line 13, in __init__
    self.UI.ajoutePlateau(self)
AttributeError: 'NoneType' object has no attribute 'ajoutePlateau'

Tags: the代码selfnoneuiinitdef错误
1条回答
网友
1楼 · 发布于 2024-07-04 16:45:56

正如@Jacques Gaudin所注意到的,您需要改变这一点:

class ZombristHash(Hash):
    def __init__(self):
        Hash.__init__()

为此:

class ZombristHash(Hash):
    def __init__(self):
        super().__init__()

UI也是如此

最后,你会得到这样的结果:

class Plateau:
    def __init__(self, j1, j2, UI, Hash):
        self.UI = UI
        self.UI.ajoutePlateau(self)
        self.Hash = Hash

class Hash:
    def __init__(self): 
        pass

class ZombristHash(Hash):
    def __init__(self):
        super().__init__()

class UI:
    def __init__(self):
        self.Plateau = None

    def ajoutePlateau(self, Plateau):
        self.Plateau = Plateau

class UITerminal(UI):
    def __init__(self):
        # super(UITerminal, self).__init__()
        super().__init__()

class Humain():
    def __init__(self, j):
        pass

p = Plateau(Humain("j1"), Humain("j2"), UITerminal(), ZombristHash())

相关问题 更多 >

    热门问题