Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

super in python 2.7 [duplicate]

I'm trying to understand how to use super in 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()

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

I'm confused about why I cannot use super(student,self).__init__(self,n,a,w) in this case, and why I have to use people.__init__(self,n,a,w)

Any help?

like image 837
ikel Avatar asked Jun 28 '16 09:06

ikel


People also ask

What is super () __ Init__ in Python?

The “__init__” is a reserved method in python classes. It is known as a constructor in Object-Oriented terminology. This method when called, allows the class to initialize the attributes of the class. Python super() The super() function allows us to avoid using the base class name explicitly.

How does super () work in Python?

The super() function in Python makes class inheritance more manageable and extensible. The function returns a temporary object that allows reference to a parent class by the keyword super. The super() function has two major use cases: To avoid the usage of the super (parent) class explicitly.

Can we use super in multiple inheritance in Python?

In fact, multiple inheritance is the only case where super() is of any use. I would not recommend using it with classes using linear inheritance, where it's just useless overhead.

Can a subclass have two superclasses Python?

In Python a class can inherit from more than one class. If a class inherits, it has the methods and variables from the parent classes. In essence, it's called multiple inheritance because a class can inherit from multiple classes.


1 Answers

Your base class people should be derived from the object class, to make it a new-style class, which will allow super() to work.

You should then use super as:

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

Old-style classes behave quite differently, and I don't understand them

like image 82
Simon Callan Avatar answered Oct 26 '22 04:10

Simon Callan