Jenkins
Jenkins

Reputation: 37

Using class method to search list of objects - python

I'm trying to search a list of objects (employees) using this function. Basically, the user can input an employee ID # and then I want to use that ID to see if any of the employee's ID #'s match and return all variables of the object that it matches. I thought this would do what I needed but something isn't quite right. Any thoughts?

def getByID(employees, eid):
    readFile(employees)
    for x in employees:
        y = x.getEmployeeID
        if y == eid:
            return x.printObject()

Upvotes: 0

Views: 1898

Answers (1)

U13-Forward
U13-Forward

Reputation: 71610

You're only returning the first match, because return only executes and evaluates once, so you may well need to do (the below returns a list), also have to call getEmployeeID:

def getByID(employees, eid):
    l=[]
    readFile(employees)
    for x in employees:
        y = x.getEmployeeID()
        if y == eid:
            l.append(x.printObject())
    return l

Upvotes: 1

Related Questions