无法获得输出

时间:2019-10-25 03:17:02

标签: python python-3.x

您好,我是python的新手,只是想逐步了解Python,因此我创建了一个类并在其下创建了两个方法,但我不明白为什么下面的代码不起作用。有什么帮助吗?

仅供参考,我正在使用jupyter笔记本

class student:
    clg='xyz' #class variable
    def _init_(self,rollno,name):
        self.rollno=rollno
        self.name=name
    def display(self):
        print('student name',self.name)
        print('student roll no',self.rollno)
        print('student college',student.clg)

student1 = student('xyz001',"ajay")
student1.display()

错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-36-4ae2a2de8a8b> in <module>()
     10         print('student college',student.clg)
     11 
---> 12 student1 = student('xyz001',"ajay")
     13 student1.display()

TypeError: object() takes no parameters

1 个答案:

答案 0 :(得分:1)

非常简单的答案。您的'_init_'的两边都必须有双下划线,例如:__init__