gpt4 book ai didi

python - 类型错误 : __init__() takes exactly 3 arguments (2 given)

转载 作者:太空狗 更新时间:2023-10-30 00:23:40 25 4
gpt4 key购买 nike

我在这里看到了一些关于我的错误的答案,但对我没有帮助。我绝对是 Python 类(class)的新手,并且在 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)

这是我得到的错误

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

最佳答案

__init__() 定义调用 2 个输入值,startValuefirstValue。您只提供了一个值。

def __init__(self, startValue, firstValue):

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

# Something like
a = SimpleCounter(5, 5)

现在,您是否真的需要 2 个值是另一回事了。 startValue 仅用于设置 firstValue 的值,因此您可以重新定义 __init__() 以仅使用一个:

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


a = SimpleCounter(5)

关于python - 类型错误 : __init__() takes exactly 3 arguments (2 given),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9444992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com