简体   繁体   English

Python TypeError:object()没有参数错误

[英]Python TypeError: object() takes no parameters error

I'm a beginner in Python. 我是Python的初学者。 I'm following "Learn Python In The Hard Way". 我正在关注“艰苦学习Python”。 In Exercise 40, I tried to write a short code but got an error. 在练习40中,我尝试编写一个简短的代码,但出现错误。 Please help me :( 请帮我 :(

Source 资源

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()

Debug 除错

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

The cause is that __init__ is misspelled :-) 原因是__init__拼写错误:-)

After that, there will be a different error message because showInfo("JJJ") only passes in one parameter when three are needed showInfo(somename, somephone, someage) . 此后,将出现不同的错误消息,因为showInfo("JJJ")仅在需要三个showInfo(somename, somephone, someage)时才传入一个参数。

After that, there will be yet one more message because the last three methods are not properly indented under the class definition. 此后,将再有一条消息,因为后三个方法在类定义下未正确缩进。

Here is the fixed-up code: 这是固定的代码:

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()

This outputs: 输出:

Name: Tom

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM