【问题标题】:Constructor failed to take input arguments [closed]构造函数未能接受输入参数[关闭]
【发布时间】:2013-05-18 06:46:41
【问题描述】:

我是 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();

以下是错误

Traceback(最近一次调用最后一次):文件“main.py”,第 14 行,在 student1 = StudentData('Ali',12,90) TypeError: 这个构造函数需要 没有参数

谁能解释我为什么会收到这个错误。

很抱歉问了一个蹩脚的问题:(

【问题讨论】:

    标签: python linux class error-handling constructor


    【解决方案1】:

    __init__() 的两边应该有两个下划线,因此 python 将您的下划线视为您班级中的普通函数。

    变化:

    def _init_(self,name,age,marks):
    

    def __init__(self,name,age,marks):
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多