Epalissad
Epalissad

Reputation: 451

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.

How do I iterate through all employee objects - in this case 3 employees that have been created? The GetLocations method below only prints the managers location. 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'])

Upvotes: 0

Views: 5281

Answers (5)

Razer
Razer

Reputation: 8211

I would add a static list of locations to the Employeeclass:

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

Upvotes: 1

Jochen Ritzel
Jochen Ritzel

Reputation: 107746

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

Upvotes: 0

platinummonkey
platinummonkey

Reputation: 808

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)

Upvotes: 0

Roman Bodnarchuk
Roman Bodnarchuk

Reputation: 29747

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

Upvotes: 2

DSM
DSM

Reputation: 353489

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. 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. 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.

Upvotes: 1

Related Questions