0

There is a class called "Branch" in which I want to have employees stored as an Array and not List.

from com.gs.entities.Employee import Employee
from _datetime import datetime, timedelta
import array as arr

class Branch:

    def __init__(self, branchName):
        self.branchName = branchName
        self.startTime = datetime.now()
        self.endTime = datetime.now() + timedelta(hours=9)
        #=======================================================================
        # self.employees = []
        # for i in range(0, 10):
        #     if(i < 6):
        #         self.employees.append(Employee(i, str(i), "Cashier"))
        #     elif(i < 8):
        #         self.employees.append(Employee(i, str(i), "Loan Officers"))
        #     elif(i == 8):
        #         self.employees.append(Employee(i, str(i), "Deputy Manager"))
        #     elif(i == 9):
        #         self.employees.append(Employee(i, str(i), "Manager"))
        #=======================================================================
        self.employees = arr.a
        
    def startOperation(self):
        print("Starting operation at " + self.startTime)

    def endOperation(self):
        print("Starting operation at " + self.endTime)


b = Branch("CP")
print(str(type(b.employees)))
print(*b.employees, sep="\n")

The commented out code when un-commented will print type of branch.employees as list. Now I wanted to store them as arrays (fixed size) therefore when they are printed on type, it should print "array". So I am trying to use arr.array BIF and it accepts as 'typecode' its very first argument. What is the typecode in this case ? (As I can only see a certain enumerated typecodes which one can pass)

userx
  • 3,713
  • 5
  • 32
  • 38
  • 2
    The `array` module can only store numerical data, not user-defined objects. Just use a normal list instead – SuperStormer Mar 16 '21 at 02:54
  • @SuperStormer - But how do ensure a fixed size given that list is editable. – userx Mar 16 '21 at 02:56
  • You can't append to an array in any language, so I don't know how you expect this code to work. – SuperStormer Mar 16 '21 at 02:57
  • 2
    `array` module designed to save data in native types, so `int` takes 4 bytes of ram instead of 28 (on x64) or `float` use 8 bytes instead of 24. If you want to disallow collection update (add/remove/replace/etc.) use `tuple` – JL0PD Mar 16 '21 at 02:58
  • 2
    Unlike in some other languages it is not a typical Python habit to put obstacles into the path of the developers. If you don't want to change the size of a list, then you simply don't do it. No need to enforce that by type. – Klaus D. Mar 16 '21 at 03:00
  • 1
    Also if you want to enforce more rules in your code use [property](https://docs.python.org/3/library/functions.html?highlight=property#property) and [slots](https://stackoverflow.com/questions/472000/usage-of-slots) – JL0PD Mar 16 '21 at 03:18
  • Note, `array.array` objects support `.append` anyway... – juanpa.arrivillaga Mar 16 '21 at 04:16

0 Answers0