Resignation function invalidates the index value of other employee objects stored in a list

Question:

I have a class ResignationSystem. I also have an Employee and Company class.


class ResignationSystem:
    def accept_resignation(self, employee):
            self.employee_list.pop(employee.employee_num)

    def resignation_from_employee(self, employee):
        resign = self.accept_resignation(employee)
        if resign:
            self.employee_del_attr(employee)

        else:
            print("Resignation declined.")
        
    def employee_del_attr(self, employee):
        del employee.company
        del employee.salary
        del employee.employee_num
        employee.has_job = False
class Company(ResignationSystem):
    def __init__(self, name):
        self.minimum_hiring_grades_percent = 90
        self.employee_max_leaves = 30
        self.name = name

    employee_list = []

Employee Class

class Employee:
    def __init__(self, name, number, grades):
        self.name = name
        self.number = number
        self.working_days = 300
        self.bonus_percent = 30
        self.has_job = False
        self.grades_percent_average = grades
        self.employee_num = None

    def resign_from_company(self):
        self.company.resignation_from_employee(self)

Each employee has a unique employee_num which is the index of its dictionary in the employee_list. When popping the employee dictionary from employee_list of Company, the employee_num of all employees ahead of the popped employee are invalidated. Is there anyway I can avoid this, or then change all employee_num

An example of what the employee_list may look like:

[
{'name': 'Rob', 'number': 9606239371, 'working_days': 300, 'bonus_percent': 30, 'has_job': True, 'grades_percent_average': 90, 'employee_num': 0, 'available_leaves': 30, 'salary_dollars': 90000, 'company': 'EY'},

 {'name': 'Steve', 'number': 9980083117, 'working_days': 300, 'bonus_percent': 30, 'has_job': True, 'grades_percent_average': 91, 'employee_num': 1, 
'available_leaves': 30, 'salary_dollars': 91000, 'company': 'EY'}
]

If:

rob.resign_from_company

Then, the dictionary of rob, which is at index value 0, is popped. But, the employee_num of steve now doesn’t correspond to its index value. This causes errors in other functions which aren’t required for the scope of this question

Asked By: PythonProgrammer

||

Answers:

You could use a dictionary instead of a list to store your employees, using employee_num as the keys of that dictionary.

your employee_list would look like:

{
    0 : {'name': 'Rob', 'number': 9606239371, 'working_days': 300, 'bonus_percent': 30, 'has_job': True, 'grades_percent_average': 90, 'employee_num': 0, 'available_leaves': 30, 'salary_dollars': 90000, 'company': 'EY'},
    1 : {'name': 'Steve', 'number': 9980083117, 'working_days': 300, 'bonus_percent': 30, 'has_job': True, 'grades_percent_average': 91, 'employee_num': 1, 'available_leaves': 30, 'salary_dollars': 91000, 'company': 'EY'}
}

Alternatively you could look at "real" databases.

Answered By: Taek
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.