【设计开发】 Python学习笔记-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()

 

以上代码第23行报错:

➜  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

 

参考做法:https://blog.csdn.net/andos/article/details/8973368

将Person改为新类,问题解决。

 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)

 

posted @ 2019-01-20 15:56  digital-world  阅读(3069)  评论(0编辑  收藏  举报