gpt4 book ai didi

python - 类型错误 : object() takes no parameters

转载 作者:行者123 更新时间:2023-12-03 08:22:59 26 4
gpt4 key购买 nike

这是我的课:

    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

我究竟做错了什么?
谢谢和欢呼!

最佳答案

object构造函数不接受额外的参数。 __new__的正确执行方法不应传递最后两个参数:

def __new__(cls, first, last):
print("Calling __new__() method of class {}".format(cls))
return object.__new__(cls)

关于python - 类型错误 : object() takes no parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51470075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com