超级python 2.7

ike*_*kel 6 python subprocess python-2.7 python-3.x

我试图了解如何super在python中使用

class people:   
 name = ''  
 age = 0  
  __weight = 0  

 def __init__(self,n,a,w):  
    self.name = n  
    self.age = a  
    self.__weight = w  
def speak(self):  
    print("%s is speaking: I am %d years old" %(self.name,self.age))  


class student(people):  
 grade = ''  
 def __init__(self,n,a,w,g):  
    #people.__init__(self,n,a,w)  
    super(student,self).__init__(self,n,a,w)
    self.grade = g  

 def speak(self):  
    print("%s is speaking: I am %d years old,and I am in grade %d"%(self.name,self.age,self.grade))  


s = student('ken',20,60,3)  
s.speak()
Run Code Online (Sandbox Code Playgroud)

上面的代码出现以下错误:

---------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-147-9da355910141> in <module>()
     10 
     11 
---> 12 s = student('ken',20,60,3)
     13 s.speak()

<ipython-input-147-9da355910141> in __init__(self, n, a, w, g)
      3     def __init__(self,n,a,w,g):
      4         #people.__init__(self,n,a,w)
----> 5         super(student).__init__(self,n,a,w)
      6         self.grade = g
      7 

TypeError: must be type, not classobj
Run Code Online (Sandbox Code Playgroud)

我很困惑为什么我不能super(student,self).__init__(self,n,a,w)在这种情况下使用,为什么我必须使用people.__init__(self,n,a,w)

有帮助吗?

Sim*_*lan 18

您的基类people应该从object类派生,以使其成为一个新的类,这将允许super()工作.

然后你应该super用作:

super(student, self).__init__(n,a,w)
Run Code Online (Sandbox Code Playgroud)

旧式课程表现完全不同,我不明白