TypeError:描述符'__init__'需要'超级'对象但收到'str'

时间:2018-01-20 07:29:06

标签: python python-3.x python-2.7

使用Python 3.X作为解释器。我刚刚继承了Employee类,其中两个派生类为DeveloperManager

以下代码引发了类型错误:'__init__' requires a 'super' object but received a 'str' 我不明白为什么会发生这种情况,我还没有发现该程序存在任何问题。

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()

错误跟踪:

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'

3 个答案:

答案 0 :(得分:6)

将所有stringEquals来电更改为super.__init__(...)。问题是缺少一对括号super().__init__(...)

从官方Python 3.3参考here开始, super 实际上是一个带有签名()的内置函数。

答案 1 :(得分:6)

{% for file in c.files.all %} <li><a href='{{ MEDIA_URL }}{{ file.upploaded_file.url }}'>download</a></li> {% endfor %} 课程中发出错误的super来电应该是:

Developer

super(Developer, self).__init__(f, l, a) 课程中:

Manager

您还有其他问题,例如,在super(Manager, self).__init__(f, l, a) 中,您有两个名为Employee的属性,一个是raise_amount,另一个是方法(函数)。这是不允许的,float优先,因此float失败。

dev1.raise_amount()中你拼写错误print_employee()

答案 2 :(得分:1)

def __init__(self,millage,cost,name,tyre,hp):
    super().__init__(millage,cost)
    self.name=name
    self.tyre=tyre
    self.hp=hp

super() 你忘了放 ()