Search code examples
pythonlistclasssubclasssuperclass

How to access specific objects in a list (python)


I have written a superclass People and from this I have two subclasses called employee and patient , from employee there are two subclasses called doctor, and staff.

The attributes for People include : name, address, and date of birth

Employee: name, address, date of birth, employee number, and date of hire

patient: name, address, date of birth, MCP, date of admission, hospital name, doctor's name, and room number

Doctor: name, address, date of birth, employee number, date of hire, annual salary, specialization, and number of patients

Staff: name, address, date of birth, employee number, date of hire, hourly salary, and full time or part time status

We were asked to create a text file in which we put our data for each person from each class and then import the data into python and store the objects of the five class types into a 1d list.

I have completed this part, however I now need to access certain class types to do calculations. For example, I need to access staff to calculate annual pay from hourly pay and I need to access doctor to get the annual salary, but I'm not sure how to do this.

When I try to run the code I have written I get an error message saying

print(people[i].getHSalary())
AttributeError: 'Person' object has no attribute 'getHSalary'

My error is in my for loop at the end. What can I use to access certain attributes of each object without getting this error? There are several things I need to calculate that are independent of certain objects.

from Person import Person
from Employee import Employee
from Doctor import Doctor
from Staff import Staff
from Patient import Patient

def main():
    people = []
    infile = open("classdata.txt", "r")
    allLines = infile.readlines()
    for i in range(len(allLines)):
        line = allLines[i].strip()

        if line == "Person":
            i += 1
            line = allLines[i].strip()
            name = line
            i += 1
            line = allLines[i].strip()
            address = line
            i += 1
            line = allLines[i].strip()
            DOB = line

            person = Person(name, address, DOB)
            people.append(person)

        elif line == "Employee":
            i += 1
            line = allLines[i].strip()
            name = line
            i += 1
            line = allLines[i].strip()
            address = line
            i += 1
            line = allLines[i].strip()
            DOB = line
            i += 1
            line = allLines[i].strip()
            empNum = line
            i += 1
            line = allLines[i].strip()
            DOH = line

            emp = Employee(name, address, DOB, empNum, DOH)
            people.append(emp)

        elif line == "Doctor":
            i += 1
            line = allLines[i].strip()
            name = line
            i += 1
            line = allLines[i].strip()
            address = line
            i += 1
            line = allLines[i].strip()
            DOB = line
            i += 1
            line = allLines[i].strip()
            empNum = line
            i += 1
            line = allLines[i].strip()
            DOH = line
            i += 1
            line = allLines[i].strip()
            aSal = line
            i += 1
            line = allLines[i].strip()
            spec = line
            i += 1
            line = allLines[i].strip()
            patients = line
            doc = Doctor(name, address, DOB, empNum , DOH, aSal, spec, patients)
            people.append(doc)

        elif line == "Staff":
            i += 1
            line = allLines[i].strip()
            name = line
            i += 1
            line = allLines[i].strip()
            address = line
            i += 1
            line = allLines[i].strip()
            DOB = line
            i += 1
            line = allLines[i].strip()
            empNum = line
            i += 1
            line = allLines[i].strip()
            DOH = line
            i += 1
            line = allLines[i].strip()
            jTitle = line
            i += 1
            line = allLines[i].strip()
            dept = line
            i += 1
            line = allLines[i].strip()
            hSalary = line
            i += 1
            line = allLines[i].strip()
            status = line

            staff = Staff(name, address, DOB, empNum, DOH, jTitle, dept, hSalary, status)
            people.append(staff)

        elif line == "Patient":
            i += 1
            line = allLines[i].strip()
            name = line
            i += 1
            line = allLines[i].strip()
            address = line
            i += 1
            line = allLines[i].strip()
            DOB = line
            i += 1
            line = allLines[i].strip()
            MCP = line
            i += 1
            line = allLines[i].strip()
            dateAdmit = line
            i += 1
            line = allLines[i].strip()
            hosp = line
            i += 1
            line = allLines[i].strip()
            docName = line
            i += 1
            line = allLines[i].strip()
            roomNum = line

            pat = Patient(name, address, DOB, MCP, dateAdmit, hosp, docName, roomNum)
            people.append(pat)


    for i in range(len(people)):
        print("%s \n" % people[i])
        if type(staff) is Staff:
        print(people[i].getHSalary())


main()

Solution

  • At first 'if' case you create Person object and you add this object to the list - at this moment your list contains instances of 'Person' class. List in Python can contain only one type of objects so when you try to invoke 'getHSalary', which is not a part of Person class, error occurs.