简体   繁体   English

Python类构造函数不起作用,给出了太多参数

[英]Python class constructor not working, too many arguments given

I didn't write "Django" in the question cause I don't think it is relevant. 我没有在问题中写“ Django”,因为我认为这不相关。 I have a Django Test that starts like this: 我有一个以如下方式开始的Django测试:

class APITests(APITestCase):

    def __init__(self):

        self.token = ""
        self.fixtures = ['tests/testdata.json']
        super(APITests, self).__init__()

It doesn't work, it gives me the error: 它不起作用,它给了我错误:

TypeError: __init__() takes exactly 1 argument (2 given)

Could anyone explain me why? 谁能解释我为什么? how can I solve this? 我该如何解决?

I don't think you should actually override __init__ for APITestCase . 我认为您实际上不应该为APITestCase覆盖__init__ I think they provide special setup and teardown methods that can be overridden. 我认为它们提供了可以覆盖的特殊setupteardown方法。

But if you really have to override it, make sure you accept all arguments that are passed by the testing framework: 但是,如果确实需要重写它,请确保接受测试框架传递的所有参数:

class APITests(APITestCase):
    def __init__(self, *args, **kwargs):
        self.token = ""
        self.fixtures = ['tests/testdata.json']
        super(APITests, self).__init__(*args, **kwargs)

or define them as class attributes: 或将它们定义为类属性:

class APITests(APITestCase):
    tokens = ""
    fixtures = ('tests/testdata.json')
    ...

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

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