Tag Archives: -super() argument 1 must be type, not classobj

[design and development] Python learning notes – Super () argument 1 must be type, not classobj

 1 #!/usr/bin/python
 2 # -*- coding:UTF-8 -*-
 3 
 4 class Person():
 5   "A Person class"
 6 
 7   def __init__(self,name,age):
 8     self.name =name
 9     self.__age = age
10 
11   def Introduce(self):
12     print "Hi, My name is:%s,I'm %d years-old."%(self.name,self.__age)
13 
14 
15 class Student(Person):
16   "A student class <- Person"
17 
18   def __init__(self,name,age,score):
19     Person.__init__(self,name,age)  #Notice:If don't call person.__init__ then Student class will lose name and age.
20     self.score = score
21   
22   def ReportScore(self):
23     super(Student,self).Introduce()
24     print "My score is:%d"%(self.score)
25 
26   def __str__(self):
27     return "This is a student class"
28 
29 
30 s1=Student('A',20,80)
31 
32 #s1.Introduce()
33 s1.ReportScore()

Error on line 23 of the above code:

➜  Python git:(master) ✗ python oop_syntax.py
Traceback (most recent call last):
  File "oop_syntax.py", line 33, in <module>
    s1.ReportScore()
  File "oop_syntax.py", line 23, in ReportScore
    super(Student,self).Introduce()
TypeError: super() argument 1 must be type, not classobj

Reference practice: https://blog.csdn.net/andos/article/details/8973368

Change person to a new class to solve the problem

 1 #!/usr/bin/python
 2 # -*- coding:UTF-8 -*-
 3 
 4 class Person(object):
 5   "A Person class"
 6 
 7   def __init__(self,name,age):
 8     self.name =name
 9     self.__age = age
10 
11   def Introduce(self):
12     print "Hi, My name is:%s,I'm %d years-old."%(self.name,self.__age)