簡體   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