Reputation: 1130
Below is my sample code on Python Inheritance.
class db_Conn:
hike = 1.04
def __init__(self,first,last,pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@ibm.com'
def full_name(self):
return'{} {}'. format( self.first, self.last)
def emp_raise(self):
self.pay = int(self.pay * self.hike)
emp1 = db_Conn('amitesh','sahay',50000)
emp2 = db_Conn('amit','sharma',60000)
class Dev(db_Conn):
def __init__(self,first,last,pay,prog):
super().__init__(first,last,pay)
self.prog = prog
dev1 = Dev('amitesh','sahay',50000, 'python')
dev2 = Dev('amit','sharma',60000,'scala')
print (dev1.prog)
print(dev2.email)
I am getting below error::
Traceback (most recent call last):
dev1 = Dev('amitesh','sahay',50000, 'python')
super().__init__(first,last,pay)
TypeError: super() takes at least 1 argument (0 given)
I am not able to figure out what mistake am I doing. Please help....!!!
Upvotes: 0
Views: 1261
Reputation: 51
For inheritance to be enabled in Python 2.7, the connection between the parent class and child class needs to be established by passing two arguments. These arguments are a reference to the child class and the self object. Thus the super function should be written as:
super(Dev, self).__init__(first,last,pay)
Also for inheritance in python 2.7, your parent class should inherit from object.
Upvotes: 0
Reputation: 7840
The documentation for super()
shows it needs at least one argument: the class to start searching from. This was made optional in Python 3, but as you're using 2.7, you'll need:
super(Dev).__init__(first,last,pay)
It also says:
Note:
super()
only works for new-style classes.
New-style classes inherit from object
, which yours doesn't. You'll want to declare your db_Conn
class with:
class db_Conn(object):
Upvotes: 1