类和对象的问题.__ new __()没有参数

时间:2013-05-15 21:44:37

标签: python python-3.x

class Factor:
    def __int__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

a = input("What is A?")
a = int(a)
b = input("What is B?")
b = int(b)
c = input("What is C?")
c = int(c)

e = Factor(a,b,c)

这是我为我创建的任何课程返回的错误

Traceback (most recent call last):
  File "C:\Users\Alex\Desktop\Alex Factoring Extra Credit.py", line 37, in <module>
    e = Factor(a,b,c)
TypeError: object.__new__() takes no parameters

对于我制作的任何课程都会发生这种情况,我到处寻找,卸载并重新安装,但我找不到解决方案。我有复制和粘贴的课程,我在其他地方找到并且那些可以工作,但我的工作即使它完全一样。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:4)

您没有正确命名__init__,忘记了i

class Factor:
    def __init__(self, a, b, c):

如果没有__init__()方法,参数将被发送到父object.__new__()方法,该方法不带任何参数。

Python 3.3上的演示(稍微更新的错误消息):

>>> class Factor:
...     def __int__(self, a, b, c):
...         self.a = a
...         self.b = b
...         self.c = c
... 
>>> Factor(1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object() takes no parameters
>>> class Factor:
...     def __init__(self, a, b, c):
...         self.a = a
...         self.b = b
...         self.c = c
... 
>>> Factor(1, 2, 3)
<__main__.Factor object at 0x10a955050>