Python TypeError:object()不带参数错误

时间:2017-07-26 00:43:55

标签: python python-3.x

我是Python的初学者。我跟随"以艰难的方式学习Python"。在练习40中,我尝试编写一个简短的代码但是出错了。请帮帮我:(

来源

class showInfo(object):
    'Initialize a classL'
    def __int__(self, name, phone, age):
        self.name = name
        self.phone = phone
        self.age = age

def showName(self):
    print("Name: "+self.name)
def showAge(self):
    print("Age: "+self.age)
def showPhone(self):
    print("Phone: "+self.phone)

emp1 = showInfo("JJJ")

emp1.showName()

调试

Traceback (most recent call last):
File "classes.py", line 15, in <module>
    emp1 = showInfo("JJJ")
TypeError: object() takes no parameters

1 个答案:

答案 0 :(得分:5)

原因是__init__拼写错误: - )

之后,会出现不同的错误消息,因为showInfo("JJJ")仅在需要三个showInfo(somename, somephone, someage)时传入一个参数。

之后,还会有一条消息,因为最后三个方法没有在类定义下正确缩进。

以下是修正后的代码:

class showInfo(object):
    'Initialize a classL'
    def __init__(self, name, phone, age):
        self.name = name
        self.phone = phone
        self.age = age

    def showName(self):
        print("Name: "+self.name)

    def showAge(self):
        print("Age: "+self.age)

    def showPhone(self):
        print("Phone: "+self.phone)

emp1 = showInfo("Tom", "555-1212", 21)
emp1.showName()

输出:

Name: Tom