繁体   English   中英

TypeError:__init __()缺少1个必需的位置参数:分支,但提供了参数

[英]TypeError: __init__() missing 1 required positional argument: branches, but argument is supplied

我有另一个类的子类。 __init__方法都具有相同的签名。 我的呼叫者提供了所有必需的参数。 但是,我仍然收到缺少的参数错误。

我尝试了不同的方法来调用超类的__init__方法。

基类初始化函数(从另一个文件导入):

class Gerrit:
    connections = {
        'consumer': MyGerritClient(host='gerrit.consumer.com', username=getpass.getuser()),
        'b2b': MyGerritClient(host='gerrit.b2b.com', username=getpass.getuser())
    }

    def __init__(self, segment, rev_id, branches):
        print("TRACE: Gerrit::__init__()")
        self.branches = branches
        self.review = None  # Type: GerritChange
        self.gerrit_client = self.connections[segment]
        for review_candidate in self.gerrit_client.query(rev_id):
            if self.branch_is_valid(review_candidate.branch) and review_candidate.status != 'ABANDONED':
                self.review = review_candidate
        self.approved = False
        self.rev_id = rev_id
        self.merged = False

子类__init__方法:

class CvGerrit(Gerrit):
    def __init__(self, segment, rev_id, branches):
        Gerrit.__init__(segment, rev_id, branches)

呼叫者:

review = CvGerrit(segment='consumer', rev_id=gerrit_id, branches=my_branches)

我希望可以构造子类CvGerrit 相反,我得到缺少的位置参数错误。 在调用方传递dict ,基类的其他用法为branchs参数获取list是否重要? 这是有意的,也是产生子类的全部原因,因此我可以使用子级中的重写函数以不同方式处理此集合。

您将要打电话给

Gerrit.__init__(self, segment, rev_id, branches)

要么

super().__init__(segment, rev_id, branches)

__init__本身就是另一个函数对象,因此当您通过类访问它时,必须传递所有参数,包括self 通过实例或super访问它时,将获得一个绑定方法,该方法的self已经预先插入到参数列表中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM