Ays*_*yse 0 python linux error-handling constructor class
我是Python的新手.我一直在关注有关python类的在线教程,但是我得到了一个奇怪的错误.
我无法弄清楚我做了什么.
以下是我的代码:
class StudentData:
"Contains information of all students"
studentNumber = 0;
def _init_(self,name,age,marks):
self.name = name;
self.age = age;
self.marsk = marks;
def displayStudentNUmber(self):
print 'Total Number of students = ',studentNumber;
def displayinfo(self):
print 'Name of the Student: ',name;
print 'Age of the Student: '.age;
print 'Marks of the Student: '.marks;
student1 = StudentData('Ayesha',12,90)
student2 = StudentData('Sarah',13,89)
print "Student number in case of student 1",student1.displayStudentNumber();
print "Information of the Student",student1.dispalyinfo();
print "Student number in case of student 1",student2.displayStudentNumber();
print "Information of the Student",student2.dispalyinfo();
Run Code Online (Sandbox Code Playgroud)
以下是错误:
回溯(最近一次调用最后一次):文件"main.py",第14行,在student1 = StudentData('Ali',12,90)TypeError:此构造函数不带参数
任何人都可以解释为什么我会收到此错误.
抱歉蹩脚的问题:(
__init__() 应该在两边都有两个下划线,因此python将你的对象视为你班级中的普通函数.
更改:
def _init_(self,name,age,marks):
Run Code Online (Sandbox Code Playgroud)
至
def __init__(self,name,age,marks):
Run Code Online (Sandbox Code Playgroud)