TypeError: object() takes no parameters when creating an object [closed]

筅森魡賤 提交于 2021-02-05 11:44:24

问题


So first of all, I know there is a bunch of answers here already relating this question, but I couldn't find the right one for my problem. When trying to create an object i basically just get this error. If any answers, thanks in advice.

Here's my code:

class Human:
    __name = None
    __height = 0

def __init__(self, name, height):
    self.__name = name
    self.__height = height

def set_name(self, name):
    self.__name = name

def get_name(self):
    return self.__name

def set_height(self, height):
    self.__height = height

def get_height(self):
    return self.__height

def get_type(self):
    print('Human')

def toString(self):
    return '{} is {} cm tall.'.format(self.__name,
                                      self.__height)

person = Human('Corey', 180)

回答1:


Most common cause: misspelled __init__()

The usual cause of this error is that the __init__() method has been misspelled, usually by forgetting one of the two leading or trailing underscores:

>>> class A:
        def __init_(self, x, y):
            self.x = x
            self.y = y

>>> A(10, 20)
Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    A(10, 20)
TypeError: object() takes no parameters

Less common cause: mis-indentiation

The other cause is mis-indentation where the __init__() method is not indented to be inside of the class definition:

>>> class B:
        """Example class"""

>>> def __init__(self, p, q):
        self.p = p
        self.q = q

>>> B(30, 40)
Traceback (most recent call last):
  File "<pyshell#41>", line 1, in <module>
    B(30, 40)
TypeError: object() takes no parameters



回答2:


Your class declaration has to be

class Human(object):


来源:https://stackoverflow.com/questions/45250605/typeerror-object-takes-no-parameters-when-creating-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!