What if you have a 'Employees' class and you want to list all the
employees. Currenlty i'm seeing to possibilities:
- create a 'listAll' function inside the class which returns all the
employees in a array.
- create multiple instances, putting them in a array, by calling the
Employees class multiple times in a loop (thus not creating a listAll
function in the class).
What is the better way of doing this? And should a class always
reference only on 'item'?
Bottom-up responses...
Unless you are contaminated by Java's need for a class to hold
static functions, classes are just templates for an undefined object.
You create an instance of the class to create a defined object. It is
not common in Python to create a class that is not meant to be
instantiated at least once (something that is just static methods is
probably easier implemented as an import module with plain functions and
module level globals).
Now, by "name", an "Employees" (plural s) class, to me, implies a
class to represent a group of employees -- as a group! It would NOT have
methods or members (both of which are just attributes in generic Python
hissing) that refer to information about any specific employee -- such
information should be part of an "Employee" (no S) class.
aDept = Employees(dept="Finance")
aDept.manager = Employee(name="John Dont", ID=3141596, phone="123-4567")
aDept.addEmployee(Employee(name=....))
aDept.addEmployee(Employee(...))
...
Note that this does not enforce any particular storage concept. The
Employees (with the S) class could be using a list to store each
employee, or a dictionary (maybe keyed by ID), or even an RDBM with a
join (where one has tables for, say, department, employee, and
intersection linking department to employee):
select e.* from employees as e
inner join dept_employee as de
on de.empID = e.ID
inner join departments as d
on d.id = de.deptID
where d.name = "Finance";
aBody = aDept.employee(id=....)
aDept.removeEmployee(id=...)
aDept.printEmployees()
If all you need is a "list" of raw employees, with no meaning
associated to the list... Then just use a list...
aList = []
aList.append(Employee(name=...))
aList.append(Employee(...))
for e in aList:
e.printEmployee()
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/