【发布时间】:2014-12-01 11:31:59
【问题描述】:
我需要显示已存储到列表中的值的结果。该列表存储在一个类中的一个类中,它在另一个文件中是安全的。
基本上在另一个文件上返回值,我使用了以下代码:
...
def __init__(self):
self.employee_list = []
def addEmployee(self, name, number, years_worked,salary):
newEmployee = employee.Employee(name,number,years_worked,salary)
self.employee_list.append(newEmployee)
def printEmployee(self):
for eachEmployee in self._employee_list:
print("Name",name)
print("Number",number)
print("Years Worked", years_worked)
print("Salary:", salary)
(这些值也会发送到另一个文件,其中包含有关员工的类和属性)
然后,在主文件上,为了调用函数 printEmployee 来显示所有数据,我这样写:
from staff import Staff
mystaff = Staff()
print("Welcome to my Staff Management Program")
print(choices)
elif choice == "D":
for emp in mystaff.employee_list:
print(emp)
是这样显示的:
<employee.Employee object at 0x02A2F6B0>
我可以帮忙吗? 非常感谢。
【问题讨论】:
-
不知何故您的第一段代码看起来不正确。它看起来像一个类,但缺少类定义并且缩进不正确。你能解决这个问题吗?
-
对不起,我复制并粘贴了错误的代码。请立即检查:)
-
代码很大,所以我想删掉一些与这个问题无关的代码。
-
@Ana 请查看答案,如果没有帮助,我可以修改它。
标签: python function class object attributes