super in python 2.7 -


this question has answer here:

i'm trying understand how use super in python

class people:     name = ''    age = 0     __weight = 0     def __init__(self,n,a,w):       self.name = n       self.age =       self.__weight = w   def speak(self):       print("%s speaking: %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 speaking: %d years old,and in grade %d"%(self.name,self.age,self.grade))     s = student('ken',20,60,3)   s.speak() 

the above code gets following error:

--------------------------------------------------------------------- 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 type, not classobj 

i'm confused why cannot use super(student,self).__init__(self,n,a,w) in case, , why have use people.__init__(self,n,a,w)

any help?

your base class people should derived object class, make new-style class, allow super() work.

you should use super as:

super(student, self).__init__(n,a,w) 

old-style classes behave quite differently, , don't understand them


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -