TypeError: __init__()只接受3个参数(2给定)

时间:2021-12-20 23:18:32

I've seen a few of the answers on here regarding my error but its not helped me. I am an absolute noob at classes on python and just started doing this code back in September. Anyway have a look at my code

我在这里看到了一些关于我的错误的答案,但这并没有帮助我。我绝对是python课程的新手,9月份才开始做这些代码。看看我的代码

class SimpleCounter():

    def __init__(self, startValue, firstValue):
        firstValue = startValue
        self.count = startValue

    def click(self):
        self.count += 1

    def getCount(self):
        return self.count

    def __str__(self):
        return 'The count is %d ' % (self.count)

    def reset(self):
        self.count += firstValue

a = SimpleCounter(5)

and this is the error I get

这就是我得到的误差

Traceback (most recent call last):
File "C:\Users\Bilal\Downloads\simplecounter.py", line 26, in <module>
a = SimpleCounter(5)
TypeError: __init__() takes exactly 3 arguments (2 given

2 个解决方案

#1


8  

Your __init__() definition requires both a startValue and a firstValue. So you'd have to pass both (i.e. a = SimpleCounter(5, 5)) to make this code work.

__init__()定义需要startValue和firstValue。因此,您必须同时通过这两个(即a = SimpleCounter(5,5)))才能使代码正常工作。

However, I get the impression that there's some deeper confusion at work here:

然而,我得到的印象是,这里有一些更深层次的困惑:

class SimpleCounter():

    def __init__(self, startValue, firstValue):
        firstValue = startValue
        self.count = startValue

Why do you store startValue to firstValue and then throw it away? It seems to me that you mistakenly think that the parameters to __init__ automatically become attributes of the class. That's not so. You have to assign them explicitly. And since both values are equal to startValue, you don't need to pass it to the constructor. You can just assign it to self.firstValue like so:

你为什么把startValue存储到firstValue然后把它扔掉?在我看来,您错误地认为__init__的参数会自动成为类的属性。这并不是如此。你必须显式地分配它们。由于两个值都等于startValue,所以不需要将其传递给构造函数。你可以把它赋给self。firstValue一样:

class SimpleCounter():

    def __init__(self, startValue):
        self.firstValue = startValue
        self.count = startValue

#2


10  

The __init__() definition calls for 2 input values, startValue and firstValue. You have only supplied one value.

__init__()定义要求两个输入值,startValue和firstValue。您只提供了一个值。

def __init__(self, startValue, firstValue):

# Need another param for firstValue
a = SimpleCounter(5)

# Something like
a = SimpleCounter(5, 5)

Now, whether you actually need 2 values is a different story. startValue is only used to set the value of firstValue, so you could redefine the __init__() to use only one:

现在,你是否真的需要两个值是一个不同的故事。startValue只用于设置firstValue的值,因此可以重新定义__init__(),使其只使用一个:

# No need for startValue
def __init__(self, firstValue):
  self.count = firstValue


a = SimpleCounter(5)

#1


8  

Your __init__() definition requires both a startValue and a firstValue. So you'd have to pass both (i.e. a = SimpleCounter(5, 5)) to make this code work.

__init__()定义需要startValue和firstValue。因此,您必须同时通过这两个(即a = SimpleCounter(5,5)))才能使代码正常工作。

However, I get the impression that there's some deeper confusion at work here:

然而,我得到的印象是,这里有一些更深层次的困惑:

class SimpleCounter():

    def __init__(self, startValue, firstValue):
        firstValue = startValue
        self.count = startValue

Why do you store startValue to firstValue and then throw it away? It seems to me that you mistakenly think that the parameters to __init__ automatically become attributes of the class. That's not so. You have to assign them explicitly. And since both values are equal to startValue, you don't need to pass it to the constructor. You can just assign it to self.firstValue like so:

你为什么把startValue存储到firstValue然后把它扔掉?在我看来,您错误地认为__init__的参数会自动成为类的属性。这并不是如此。你必须显式地分配它们。由于两个值都等于startValue,所以不需要将其传递给构造函数。你可以把它赋给self。firstValue一样:

class SimpleCounter():

    def __init__(self, startValue):
        self.firstValue = startValue
        self.count = startValue

#2


10  

The __init__() definition calls for 2 input values, startValue and firstValue. You have only supplied one value.

__init__()定义要求两个输入值,startValue和firstValue。您只提供了一个值。

def __init__(self, startValue, firstValue):

# Need another param for firstValue
a = SimpleCounter(5)

# Something like
a = SimpleCounter(5, 5)

Now, whether you actually need 2 values is a different story. startValue is only used to set the value of firstValue, so you could redefine the __init__() to use only one:

现在,你是否真的需要两个值是一个不同的故事。startValue只用于设置firstValue的值,因此可以重新定义__init__(),使其只使用一个:

# No need for startValue
def __init__(self, firstValue):
  self.count = firstValue


a = SimpleCounter(5)