首页 > 编程语言 > 详细

Python-TypeError: object() takes no parameters

时间:2018-05-20 17:28:29      阅读:219      评论:0      收藏:0      [点我收藏+]

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", "北门吹雪"])

 

  

 

Python-TypeError: object() takes no parameters

原文:https://www.cnblogs.com/2bjiujiu/p/9063797.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!