简体   繁体   English

TyperError:object()不带参数

[英]TyperError: object() takes no parameters

Here's my class : 这是我的课:

    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)

and I'm getting the following error that I cannot seem to resolve: 并且出现以下我似乎无法解决的错误:

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

What am I doing wrong? 我究竟做错了什么? Thank you and cheers! 谢谢您的欢呼!

The object constructor takes no additional parameters. object构造函数不需要其他参数。 The right implementation of the __new__ method should not pass the last two parameters: __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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM