AttributeError:type object';员工';没有属性';设置“增加金额”x27;用python
我不确定为什么在python中出现此错误:AttributeError:type object';员工';没有属性';设置“增加金额”x27;用python,python,Python,我不确定为什么在python中出现此错误: class Employee: raise_amount = 15 num_of_emps = 0 def __init__(self,first,last,pay): self.first = first self.last = last self.pay = pay self.email = self.first+'.'+self.last+'@gmail.co
class Employee:
raise_amount = 15
num_of_emps = 0
def __init__(self,first,last,pay):
self.first = first
self.last = last
self.pay = pay
self.email = self.first+'.'+self.last+'@gmail.com'
Employee.num_of_emps +=1
def fullname(self):
return '{}{}'.format(self.first,self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amount)
@classmethod
def set_raise_amt(cls,amount):
cls.raise_amt=amount
emp1 = Employee('Tanush','Singh',60000)
emp2 = Employee('Manush','Singh',40000)
emp3 = Employee('Darshan','Dave',80000)
emp4 = Employee('Ravi','Teja',55000)
print(emp1.fullname())
print(emp1.pay)
print(Employee.num_of_emps)
emp1.apply_raise()
print(emp1.pay)
Employee.set_raise_amt(20)
print(emp1.raise_amt())
添加装饰器后出现的错误:
Error: AttributeError: type object 'Employee' has no attribute 'set_raise_amt'
在类中定义类方法,与定义任何其他方法一样。(缩进)它们是代码中的缩进问题。函数set_raise_amt()在类之外。这就是为什么当您试图使用Employee.set_raise_amt(30)访问它时,它会给您带来错误,因为set_raise_amt()不是Employee类的一部分。
小结:更正缩进,它会工作。缩进错误。将该方法移到类中,同时删除最后一行中的
emp1.raise_amt()
。