繁体   English   中英

Python-遍历类

[英]Python - Iterate through class

嗨,我正在尝试编写一种方法,该方法可以打印报告给经理的员工的位置列表。 创建了经理对象,并为向经理报告的人员保存了一个ldap(标识)列表。

如何遍历所有员工对象-在这种情况下,已创建3位员工? 下面的GetLocations方法仅显示管理者的位置。 任何帮助,将不胜感激。 谢谢!

我想要一个输出,说: 都柏林,都柏林纽约 (格式无关)

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'])

为什么不更换

manager1 = Manager('wayneg', 'Wayne Gretzky', 'Dublin', 50000, 'active', ['axlr', 'slash', 'peterp'])

manager1 = Manager('wayneg', 'Wayne Gretzky', 'Dublin', 50000, 'active', [employee1, employee2, employee3])

然后:

def GetLocations(self):
    for emp in self.reportees:
        print emp.location

这个:

for location in [Employee]:
  print Employee.location

没有道理。 您正在创建一个列表[Employee],其中不包含雇员,但包含Employee类本身。 你想要类似的东西

for employee in self.reportees:
    print employee.location

但是您实际上并没有向Manager实例传递与员工本身的任何连接,而只是给它提供了一个名称列表。 也许像

    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

会给你你想要的。

我将静态位置列表添加到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

完整的例子:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM