Python TyperError:object()不接受任何参数
这是我的班级:Python TyperError:object()不接受任何参数,python,error-handling,Python,Error Handling,这是我的班级: class Person: def __new__(cls, first, last): print("Calling __new__() method of class {}".format(cls)) return object.__new__(cls, first, last) def __init__(self, first, last): """Constructor of Person wor
class Person:
def __new__(cls, first, last):
print("Calling __new__() method of class {}".format(cls))
return object.__new__(cls, first, last)
def __init__(self, first, last):
"""Constructor of Person working instance
(attribute initialization)"""
print("Calling __init__()")
self.first = first
self.last = last
self.age = 23
self.residency = "Lyon"
def __repr__(self):
return "Person : {} {} aged {} years living in {}".format(self.first, self.last, self.age, self.residency)
person = Person("Doe", "John")
print(person)
我得到了以下我无法解决的错误:
Calling __new__() method of class <class '__main__.Person'>
Traceback (most recent call last):
File "test.py", line 20, in <module>
person = Person("Doe", "John")
File "test.py", line 6, in __new__
return object.__new__(cls, first, last)
TypeError: object() takes no parameters
我做错了什么?
谢谢大家,干杯 对象构造函数不接受其他参数。__new__方法的正确实现不应传递最后两个参数:
def __new__(cls, first, last):
print("Calling __new__() method of class {}".format(cls))
return object.__new__(cls)
您在对象中传递了两个不需要的参数。\uuu new\uu cls,first,last。你为什么要重新定义我的类的工作实例?我只是想检查通过我的类的工作实例的创建和初始化的顺序,所以我只需要在cls上调用object而不传递参数?然后删除最后两个参数:object.\uuuuu new\uuu cls.Ok。非常感谢。干杯,巴德!