Error: TypeError: object() takes no parameters

Where?

  使用自定义类的时候,实例类的时候传递参数,提示这个错误

 

Why?

  因为类实例的时候,并不需要任何参数,但是给了类参数,本质上是类没有 __init__实例方法或者__init__实例方法并没有声明接收任何参数

 

Way?

  检查 __init__函数是否写错,init拼写错误或者 __init__函数中是否传递需要初始化的参数

 

错误代码:

class Student(object):
    # init 写错了,写成 int
    def __int__(self, student_list):
        self.student_list = student_list

    def __getitem__(self, item):
        return self.student_list[item]


students = Student(["beimenchuixue", "北门吹雪"])

 

正确代码:

class Student(object):
    # 把 int 改为 init
    def __init__(self, student_list):
        self.student_list = student_list

    def __getitem__(self, item):
        return self.student_list[item]


students = Student(["beimenchuixue", "北门吹雪"])

 

  

 

相关文章:

  • 2021-11-11
  • 2022-12-23
  • 2022-01-15
  • 2021-12-27
  • 2021-07-14
  • 2021-07-28
  • 2022-01-22
猜你喜欢
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2021-10-02
  • 2022-12-23
相关资源
相似解决方案