Using Python 3.X as the interpreter. I just had inherited the Employee
class with two of the derived class as Developer
and Manager
.
The below code is throwing a Type Error: '__init__' requires a 'super' object but received a 'str'
.
I'm not getting why exactly it's happening, I don't find any problem with the program yet.
class Employee:
raise_amount=1.05
emp_count=0
def __init__(self,first_name,last_name, amount):
self.first_name=first_name
self.last_name=last_name
self.amount=amount
self.email_id="{0}.{1}@{1}.com" .format(first_name,last_name)
Employee.emp_count +=1
def fullname(self):
print ("%s %s"%(self.first_name,self.last_name))
def print1(self):
print (self.email)
print ("Total no of Employee are :%d" %(Employee.emp_count))
def raise_amount(self):
self.amount *=self.raise_amount
return self.amount
class Developer(Employee):
raise_amount = 1.10
def __init__(self,f,l,a,prog):
super.__init__(f,l,a)
self.programming=prog
class Manager(Employee):
def __init__(self,f,l,a,emp=None):
super.__init__(f,l,a)
if emp is None:
self.my_employee=[]
else:
self.my_employee=emp
def add_employee(self,emp):
if emp in self.my_employee:
print("employee is already exist")
else:
self.my_employee.append(emp)
def remove_employee(self,emp):
if emp in self.my_employee:
self.my_employee.remove(emp)
def print_employee(self):
for emp in self.my_employee:
print (emp.fullnamme())
dev1=Developer("subhendu","panda",500000,"Python")
dev1.raise_amount()
dev2=Developer('Aditya','bishoyi',5688989,'java')
dev2.fullname()
dev1.fullname()
emp1=Employee("tonu","trip",30000)
emp1.raise_amount()
emp1.fullname()
mgr1=Manager("Biplab","choudhury",5000000)
mgr1.fullname()
mgr1.add_employee(dev1)
mgr1.add_employee(emp1)
mgr1.add_employee(dev2)
mgr1.print_employee()
mgr1.remove_employee(dev1)
mgr1.print_employee()
Error trace:
Traceback (most recent call last):
File "C:/Users/Subhendu/PycharmProjects/hello/inheritance.py", line 56, in
<module> dev1=Developer("subhendu","panda",500000,"Python")
File "C:/Users/Subhendu/PycharmProjects/hello/inheritance.py", line 27, in
init super.__init__(f,l,a) TypeError: descriptor 'init' requires a 'super'
object but received a 'str'
Change all of your super.__init__(...)
calls to super().__init__(...)
. The problem was the missing pair of parentheses ()
.
From the official Python 3.3 reference here, super is actually a built-in function with the signature super([type[, object-or-type]])
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With