[英]Python - Iterate through class
Hi I am trying to write a method that prints a list of locations of the employees which report into a manager. 嗨,我正在尝试编写一种方法,该方法可以打印报告给经理的员工的位置列表。 The manager object is created and holds a list of ldaps (id's) for the people who report to the manager. 创建了经理对象,并为向经理报告的人员保存了一个ldap(标识)列表。
How do I iterate through all employee objects - in this case 3 employees that have been created? 如何遍历所有员工对象-在这种情况下,已创建3位员工? The GetLocations method below only prints the managers location. 下面的GetLocations方法仅显示管理者的位置。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!
I would like to have an output that says: Dublin, Dublin New York (formatting is irrelevant) 我想要一个输出,说: 都柏林,都柏林纽约 (格式无关)
class Employee(object):
def __init__(self, ldap, name, location, salary, status):
self.ldap = ldap
self.name = name
self.location = location
self.salary = salary
self.status = status
class Manager(Employee):
def __init__(self, ldap, name, location, salary, status, reportees):
self.name = name
self.reportees = reportees
self.location = location
print 'Manager has been created.'
def GetLocations(self):
for location in [Employee]:
print Employee.location
employee1 = Employee('axlr', 'Axl Rose', 'Dublin', 50000, 'active')
employee2 = Employee('slash', 'Slash', 'Dublin', 50000, 'active')
employee3 = Employee('peterp', 'Peter Pan', 'New York', 50000, 'active')
manager1 = Manager('wayneg', 'Wayne Gretzky', 'Dublin', 50000, 'active', ['axlr', 'slash', 'peterp'])
Why don't replace 为什么不更换
manager1 = Manager('wayneg', 'Wayne Gretzky', 'Dublin', 50000, 'active', ['axlr', 'slash', 'peterp'])
with 同
manager1 = Manager('wayneg', 'Wayne Gretzky', 'Dublin', 50000, 'active', [employee1, employee2, employee3])
And then just: 然后:
def GetLocations(self):
for emp in self.reportees:
print emp.location
This: 这个:
for location in [Employee]:
print Employee.location
doesn't make sense. 没有道理。 You're making a list [Employee] which contains not an employee but the Employee class itself. 您正在创建一个列表[Employee],其中不包含雇员,但包含Employee类本身。 You want something like 你想要类似的东西
for employee in self.reportees:
print employee.location
but you're not actually passing your Manager instance any connection to the employees themselves, you're only giving it a list of the names. 但是您实际上并没有向Manager实例传递与员工本身的任何连接,而只是给它提供了一个名称列表。 Maybe something like 也许像
def GetLocations(self):
for employee in self.reportees:
print employee.location
employees = [Employee('axlr', 'Axl Rose', 'Dublin', 50000, 'active'),
Employee('slash', 'Slash', 'Dublin', 50000, 'active'),
Employee('peterp', 'Peter Pan', 'New York', 50000, 'active')]
manager1 = Manager('wayneg', 'Wayne Gretzky', 'Dublin', 50000, 'active', employees)
>>> manager1.GetLocations()
Dublin
Dublin
New York
would give you what you want. 会给你你想要的。
I would add a static list of locations to the Employee
class: 我将静态位置列表添加到Employee
类中:
class Employee(object):
locations = []
def __init__(self, ldap, name, location, salary, status):
self.ldap = ldap
self.name = name
self.location = location
self.locations.append(location)
self.salary = salary
self.status = status
employee1 = Employee('axlr', 'Axl Rose', 'Dublin', 50000, 'active')
employee2 = Employee('slash', 'Slash', 'Dublin', 50000, 'active')
employee3 = Employee('peterp', 'Peter Pan', 'New York', 50000, 'active')
print Employee.locations
Complete example: 完整的例子:
class Employee(object):
def __init__(self, ldap, name, location, salary, status):
self.ldap = ldap
self.name = name
self.location = location
self.salary = salary
self.status = status
class Manager(Employee):
def __init__(self, ldap, name, location, salary, status, reportees):
self.name = name
self.reportees = reportees
self.location = location
def addReportee(self, reportee):
self.reportees.append(reportee)
def GetLocations(self):
for reportee in self.reportees:
print reportee.location
employee1 = Employee('axlr', 'Axl Rose', 'Dublin', 50000, 'active')
employee2 = Employee('slash', 'Slash', 'Dublin', 50000, 'active')
employee3 = Employee('peterp', 'Peter Pan', 'New York', 50000, 'active')
manager1 = Manager('wayneg', 'Wayne Gretzky', 'Dublin', 50000, 'active', [employee1, employee2, employee3])
# and if you wanted to add more:
#manager1.addReportee(employee4)
#manager1.addReportee(employee5)
#manager1.addReportee(employee6)
class Employee(object):
def __init__(self, ldap, name, location, salary, status):
self.ldap = ldap
self.name = name
self.location = location
self.salary = salary
self.status = status
class Manager(Employee):
def __init__(self, ldap, name, location, salary, status, reportees):
self.name = name
self.reportees = reportees
self.location = location
print 'Manager has been created.'
# loop through that list of employees and print their locations
def GetLocations(self):
for employee in self.reportees:
print employee.location
employee1 = Employee('axlr', 'Axl Rose', 'Dublin', 50000, 'active')
employee2 = Employee('slash', 'Slash', 'Dublin', 50000, 'active')
employee3 = Employee('peterp', 'Peter Pan', 'New York', 50000, 'active')
# pass the employees to the manger
manager1 = Manager('wayneg', 'Wayne Gretzky', 'Dublin', 50000, 'active', [employee1,employee2, employee3])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.