错误“对象没有属性”,带有类

时间:2019-05-08 15:45:22

标签: python

嗨,我写的代码有问题。我正在尝试制作一个类并将其导入到另一个文件,但是错误不断出现。这是我的代码:

File1(带有类):

class Employee:

    def __init__(self, Number, employeeName, Address, Wage, Time):
        self.Staff = self
        self.employeeNum = Number
        self.Name = employeeName
        self._Address = Address
        self.Wage = Wage
        self.workTime = Time

    def getInfo():
        return(_employeeNum,__Address)

    def setInfo(newNumber):
        if newNumber > 6:
            self._employeeNum = newNumber

File2(导入类):

from Project7 import Employee as e

Emp1 = e.__init__("Employee", "6765", "John", "123 Baker Street", 13.00, 15)

print(Employee)
print(Emp1) 

我不确定正在发生什么,任何信息都将真正有用!预先感谢!

1 个答案:

答案 0 :(得分:0)

您不必显式调用__init__方法。

您可以简单地运行:

Emp1 = e("Employee", "6765", "John", "123 Baker Street", 13.00, 15)

然后,将Employee类重命名为“ e”:因为名称“ Employee”不存在,所以print(Employee)将引发错误。

我还建议遵循Python约定来命名类和变量: 班级资本 小写的变量

from Project7 import Employee

emp1 = Employee("Employee", "6765", "John", "123 Baker Street", 13.00, 15)