本文介绍了TypeError:创建对象时object()不接受任何参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,首先,我知道这里已经有很多答案与此问题相关,但是我找不到适合我问题的正确答案.尝试创建对象时,我基本上只会遇到此错误.如果有任何答案,请多多指教.

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.

这是我的代码:

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)

推荐答案

最常见的原因:__init __()拼写错误

此错误的常见原因是 __ init __()方法的拼写错误,通常是因为忘记了两个下划线或下划线之一:

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

少见的常见原因:缩进

另一个原因是缩进错误,其中 __ init __()方法没有缩进类定义的内部:

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

这篇关于TypeError:创建对象时object()不接受任何参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:28