类型错误:__init __()到底需要3个参数(2给出) [英] TypeError: __init__() takes exactly 3 arguments (2 given)
问题描述
我已经看到了一些问题的答案就在这里对我的错误,但它不是帮助了我。我在上Python类的绝对菜鸟刚开始在九月做这个code回来。我的code反正看看
类SimpleCounter(): 高清__init __(自我,在startValue,firstValue):
firstValue =在startValue
self.count =在startValue 高清点击(个体经营):
self.count + = 1 高清getCount将(个体经营):
回报self.count 高清__str __(个体经营):
返回'计数为%d%(self.count) 高清复位(个体经营):
self.count + = firstValue一个= SimpleCounter(5)
这是我得到的错误
回溯(最后最近一次调用):
文件C:\\用户\\比拉尔\\下载\\ simplecounter.py26行,上述<&模块GT;
一个= SimpleCounter(5)
类型错误:__init __()到底需要3个参数(给定2
您 __的init __()
定义需要的两个的一个在startValue
的和的一个 firstValue
。所以,你必须同时通过(即 A = SimpleCounter(5,5)
),使这个code的工作。
不过,我得到的IM pression这有工作一些更深层的困惑在这里:
类SimpleCounter(): 高清__init __(自我,在startValue,firstValue):
firstValue =在startValue
self.count =在startValue
为什么要保存在startValue
到 firstValue
,然后把它扔掉?在我看来,你误以为参数 __ __的init
自动成为类的属性。这是不是这样的。你必须明确地分配。而且,由于两个值等于在startValue
,你并不需要把它传递给构造函数。你可以把它分配给 self.firstValue
像这样:
类SimpleCounter(): 高清__init __(个体经营,在startValue):
self.firstValue =在startValue
self.count =在startValue
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
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
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.
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:
class SimpleCounter():
def __init__(self, startValue):
self.firstValue = startValue
self.count = startValue
这篇关于类型错误:__init __()到底需要3个参数(2给出)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!