gpt4 book ai didi

python - 类型错误 : descriptor '__init__' requires a 'super' object but received a 'str'

转载 作者:行者123 更新时间:2023-12-03 15:20:02 24 4
gpt4 key购买 nike

使用 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'

最佳答案

您的 super调用在 Developer 中给出错误类应该是:

super(Developer, self).__init__(f, l, a) 

Manager类(class):
super(Manager, self).__init__(f, l, a)

您还有其他问题,例如在 Employee 中您有两个名为 raise_amount 的属性,一个是 float一个是方法(函数)。这是不允许的, float优先,所以 dev1.raise_amount()失败。

print_employee()你拼错了 emp.fullname

关于python - 类型错误 : descriptor '__init__' requires a 'super' object but received a 'str' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48353738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com