为什么我会收到一个错误,要求使用此超级初始化函数传递一个参数

Why am I getting an error asking for one argument to be passed with this super init function

当我在 Maya 的脚本编辑器中 运行 此代码时,出现此错误:

TypeError: super() takes at least 1 argument (0 given)

我不明白什么 我的超级初始化函数需要。

google 和 youtube。我在 Maya 2018 中 运行 使用此代码。

import maya.cmds as cmds

class one:
    mod_txt = "_one_mod"         

    def __init__(self,txt):
        self.txt = txt

    def mod_txt_method(self):
        self.txt = self.txt + self.mod_txt

class two(one):
    mod_txt = "_two_mod" 

    def __init__(self,txt,txt_two):
        super().__init__(self,txt)
        self.txt_two = text_two    

ltv = two('doug','chris')

print ltv.txt
print ltv.txt_two

我认为我应该能够将新属性 txt_two 添加到我的 class、two

脚本存在一些问题。

首先,one需要subclass一些东西,在这种情况下object,否则super会失败。

接下来,为了 super 访问其继承的 __init__,您需要传递 class 和实例:super(two, self).__init__(txt)。无需将 self 传递给 __init__ 方法,只需传递方法所需的参数即可。

two__init__ 方法中也存在一个问题,其中变量 text_two 不存在(可能是打字错误?)。

现在脚本按预期执行。您还可以考虑清理脚本,使其使用标准约定:Class 名称应以大写字母开头,在模块级别使用双 spaces 分隔代码块,并使用逗号后的 space。

这是最终代码:

import maya.cmds as cmds


class One(object):

    mod_txt = "_one_mod"         

    def __init__(self, txt):
        self.txt = txt

    def mod_txt_method(self):
        self.txt = self.txt + self.mod_txt


class Two(One):

    mod_txt = "_two_mod" 

    def __init__(self, txt, txt_two):
        super(Two, self).__init__(txt)
        self.txt_two = txt_two    


ltv = Two('doug', 'chris')

print ltv.txt
print ltv.txt_two