简体   繁体   English

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

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

I have a subclass of another class. 我有另一个类的子类。 Both have the same signature for the __init__ method. __init__方法都具有相同的签名。 My caller supplies all required arguments. 我的呼叫者提供了所有必需的参数。 Yet I still get the missing argument error. 但是,我仍然收到缺少的参数错误。

I have tried different ways of calling the super class's __init__ method. 我尝试了不同的方法来调用超类的__init__方法。

Base Class init function (imported from another file): 基类初始化函数(从另一个文件导入):

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

Child Class __init__ method: 子类__init__方法:

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

Caller: 呼叫者:

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

I expect that I can construct the child class, CvGerrit . 我希望可以构造子类CvGerrit Instead I get the missing positional argument error. 相反,我得到缺少的位置参数错误。 Does it matter that the other uses of the base class take a list for the branches argument, while the caller is passing a dict ? 在调用方传递dict ,基类的其他用法为branchs参数获取list是否重要? This is intentional, and the whole reason for the subclassing, that I may process this collection differently using an overridden function in the child. 这是有意的,也是产生子类的全部原因,因此我可以使用子级中的重写函数以不同方式处理此集合。

You will want to call either 您将要打电话给

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

or 要么

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

By itself, __init__ is just another function object, so when you access it through the class, you must pass all parameters, including self . __init__本身就是另一个函数对象,因此当您通过类访问它时,必须传递所有参数,包括self When you access it through an instance or super , you get a bound method with self already pre-inserted into the argument list. 通过实例或super访问它时,将获得一个绑定方法,该方法的self已经预先插入到参数列表中。

暂无
暂无

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

相关问题 TypeError:__init __()缺少1个必需的位置参数:“ to” - TypeError: __init__() missing 1 required positional argument: 'to' TypeError:__init __()缺少1个必需的位置参数 - TypeError: __init__() missing 1 required positional argument Scrapy:TypeError:__ init __()缺少1个必需的位置参数:'settings' - Scrapy: TypeError: __init__() missing 1 required positional argument: 'settings' 类型错误:__init__() 缺少 1 个必需的位置参数:“单位” - TypeError: __init__() missing 1 required positional argument: 'units' Tweepy错误 - TypeError:__ init __()缺少1个必需的位置参数:'listener' - Tweepy error - TypeError: __init__() missing 1 required positional argument: 'listener' TypeError:__init__() 缺少 1 个必需的位置参数:'successor - TypeError: __init__() missing 1 required positional argument: 'successor 类型错误:__init__() 缺少 1 个必需的位置参数:“父” - TypeError: __init__() missing 1 required positional argument: 'parent' Kivy MDDatePicker - 类型错误:__init__() 缺少 1 个必需的位置参数:“回调” - Kivy MDDatePicker - TypeError: __init__() missing 1 required positional argument: 'callback' Flask 应用程序类型错误:__init__() 缺少 1 个必需的位置参数 - Flask Application TypeError: __init__() missing 1 required positional argument 单击 TypeError:__init__() 缺少 1 个必需的位置参数:“名称” - click TypeError: __init__() missing 1 required positional argument: 'name'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM