[英]TyperError: object() takes no parameters
這是我的課:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.