Python Forum
How are these Employee objects stored?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How are these Employee objects stored?
#1
I have a very simple program to create Employee objects:

class Employee:

  def __init__(self, id, salary):
    self.id = id
    self.salary = salary
Now lets say I built a method that would create new employee objects: emp1, emp2, emp3 ... empN. I can access these objects attributes like: emp1.id or emp5.salary. My question is, where and how are these objects being stored? Is there some list that is created or some stack that is created in memory such that when I call emp1.id it pops that objects id off the stack or something?

The reason I ask is because I am making a GUI. My GUI allows users to create new Employees and display there information but only one at a time. So I thought I would build a method that stores each newly created Employee object. However, that got me to thinking... how are they normally being stored>> do I even need to create a new list to store them or does one already exist?
Reply
#2
Maybe you can try to make your Employee class a linked list.
Python_linked_lists

Then you can iterate through the list and get only the data from the object that you want to show.
Reply
#3
You need to create some sort of storage - it may be some standard container type like list, dict, etc. It may be property of another class, e.g. Company.

class Employee:
    def __init__(self, emp_id, salary):
        self.id = emp_id
        self.salary = salary

        
    def __str__(self):
        return 'Employee, id: {}, salary: {}'.format(self.id, self. salary)
 
 
class Company:
    def __init__(self):
        self.employees = []

        
    @property
    def num_employees(self):
        return len(self.employees)

        
    def new_employee(self, emp_id, salary):
        employee = Employee(emp_id=emp_id, salary=salary)
        self.employees.append(employee)
        return employee

        
if __name__ == '__main__':
    # create company instance and add employees
    my_company = Company()
    for emp_id, salary in enumerate([1500, 1250, 1700], start=1):
        my_company.new_employee(emp_id=emp_id, salary=salary)
    
    # access employee info
    print('My company has {} employee(s):'.format(my_company.num_employees))
    for employee in my_company.employees:
        print(employee)
Output:
My company has 3 employee(s) Employee, id: 1, salary: 1500 Employee, id: 2, salary: 1250 Employee, id: 3, salary: 1700 >>>
Now, this will store employee info in memory. In order not to lose data once your program execution ends, you need to store employee info permanently on the hard drive. This may be in a database or simple text file (csv, json) or as pickle file. You will need to read/write to this hard-drive storage and keep it sync with what is in the memory.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Deleting employee from list SephMon 3 3,253 Jan-05-2021, 04:15 AM
Last Post: deanhystad
  plot multiple employee sales data in a single graph pitanshu 0 1,910 Oct-24-2019, 01:56 PM
Last Post: pitanshu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020