[英]“TypeError :super() takes at least 1 argument (0 given) ” how to solve this i have used argument as a super class name but it didn't worked
students = []
class Student:
school_name = 'Maharshi Science school'
def __init__(self,name,student_id=336):
self.name = name
self.student_id= student_id
students.append(self)
def __str__(self):
return "student: " + self.name
def get_name_capitalize(self):
return self.name.capitalize()
def get_school_name(self):
return self.school_name
class HighschoolStudent(Student):
school_name = 'Maharshi High School'
def get_school_name(self):
return "This is a high school student"
def get_name_capitalize(self):
original_value = super().get_name_capitalize()
return original_value + "-HighschoolStudent"
chirag = HighschoolStudent('chirag')
print(chirag.get_name_capitalize())
This error will only occur if you are using Python 2. To fix this, replace 仅当您使用Python 2时才会发生此错误。要解决此问题,请替换
super().get_name_capitalize()
with 与
super(HighschoolStudent, self).get_name_capitalize()
If you upgrade to Python 3, your code should work fine. 如果您升级到Python 3,您的代码应该可以正常工作。
You are getting the error due to Python 2. Please try the below code: 由于Python 2,您遇到了错误。请尝试以下代码:
students = []
class Student(object):
school_name = 'Maharshi Science school'
def __init__(self,name,student_id=336):
self.name = name
self.student_id= student_id
students.append(self)
def __str__(self):
return "student: " + self.name
def get_name_capitalize(self):
return self.name.capitalize()
def get_school_name(self):
return self.school_name
class HighschoolStudent(Student):
school_name = 'Maharshi High School'
def get_school_name(self):
return "This is a high school student"
def get_name_capitalize(self):
original_value = super(HighschoolStudent, self).get_name_capitalize()
return original_value + "-HighschoolStudent"
chirag = HighschoolStudent('chirag')
print(chirag.get_name_capitalize())
Output: Chirag-HighschoolStudent
输出:
Chirag-HighschoolStudent
There are two changes in this: 这有两个更改:
class Student
--> class Student(object)
class Student
-> class Student(object)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.