473,738 Members | 4,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OOP: How to implement listing al 'Employees'.

I was just wondering.

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'?

Thank in advance.
Dec 28 '07 #1
8 1652
Petar wrote:
What is the better way of doing this? And should a class always
reference only on 'item'?
It fully depends on what you want to do in your program. If you just
want to have a list of employees, a list or dict will suffice. If
you need a full-fledged employee database, an "Employees" class may
be a good API.

Regards,
Björn

--
BOFH excuse #83:

Support staff hung over, send aspirin and come back LATER.

Dec 28 '07 #2
On 28 dec, 13:40, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n.. .@spamgourmet.c omwrote:
Petar wrote:
What is the better way of doing this? And should a class always
reference only on 'item'?

It fully depends on what you want to do in your program. If you just
want to have a list of employees, a list or dict will suffice. If
you need a full-fledged employee database, an "Employees" class may
be a good API.

Regards,

Björn

--
BOFH excuse #83:

Support staff hung over, send aspirin and come back LATER.
It's a pure hypothetical question about where to put the function for
the list of employees when already having a Employee class.

Do I make it a method of the class, or do i create a instance of the
class for every employee (sitting inside a list)?
Dec 28 '07 #3
Bjoern Schliessmann a écrit :
Petar wrote:
>What is the better way of doing this? And should a class always
reference only on 'item'?

It fully depends on what you want to do in your program. If you just
want to have a list of employees, a list or dict will suffice. If
you need a full-fledged employee database, an "Employees" class may
be a good API.
If you need a full-fledged employee database, a RDBMS may be a good API.
And if you insist on having it the OO way, have a look at SQLAlchemy.

My 2 cents...
Dec 28 '07 #4
Petar a écrit :
On 28 dec, 13:40, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n.. .@spamgourmet.c omwrote:
>>Petar wrote:
>>>What is the better way of doing this? And should a class always
reference only on 'item'?

It fully depends on what you want to do in your program. If you just
want to have a list of employees, a list or dict will suffice. If
you need a full-fledged employee database, an "Employees" class may
be a good API.
(snip)
>
It's a pure hypothetical question about where to put the function for
the list of employees when already having a Employee class.
The problem with "pure hypothetical" questions is that they usually have
no single good answer, and sometimes even no answer at all.

Now since you're talking database, the most common patterns seems to
have either some YourDomainClass NameHere methods to query instances, or
some module/singleton object providing such querying interfaces for the
whole app (or FWIW to provide both, the first calling on the second...).
Do I make it a method of the class, or do i create a instance of the
class for every employee (sitting inside a list)?
If you have an Employee class, I don't get how you would avoid creating
an instance for each and every, well, instance ? Unless you're thinking
about something like MS "recordsets " ? But then, you don't need any
domain class at all...
Dec 28 '07 #5
Petar a écrit :
(snip)
should a class always
reference only on 'item'?
???

I'm afraid I didn't get this part of the question.

Dec 28 '07 #6
On 28 dec, 19:42, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
On Fri, 28 Dec 2007 04:05:59 -0800 (PST), Petar <peros...@gmail .com>
declaimed the following in comp.lang.pytho n:
I was just wondering.
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.addEmploy ee(Employee(nam e=....))
aDept.addEmploy ee(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.removeEmp loyee(id=...)

aDept.printEmpl oyees()

* * * * 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(Em ployee(name=... ))
aList.append(Em ployee(...))

for e in aList:
* * * * e.printEmployee ()
--
* * * * Wulfraed * * * *Dennis Lee Bieber * * * * * * * KD6MOG
* * * * wlfr...@ix.netc om.com * * * * * * wulfr...@bestia ria.com
* * * * * * * * HTTP://wlfraed.home.netcom.com/
* * * * (Bestiaria Support Staff: * * * * * * * web-a...@bestiaria. com)
* * * * * * * * HTTP://www.bestiaria.com/
I'm sorry if my question wasn't clear enough. But I think it has been
answered.

Let me explain how I got to this question. I had written een Article
class which handled the articles that I had. On a certain page I
wanted to show all the articles. That got me wondering about what to
do. Should I make a method in my Article.class which returned multiple
articles, or should I just use my current Article.class and fill a
list (with a loop) with articles. The first solution thus meaning
writing another method, the latter method to just use the current
Article.class and call it multiple times.

The post of Dennis made me realize of another solution though. To
create another class called Articles which return multiple articles.
The only problem I forsee with this that's it's gonna be a very empty
class.

Thank you all for your response, it has pointed me in the right
direction.

Dec 29 '07 #7
On Sat, 29 Dec 2007 01:53:38 -0800, Petar wrote:
The post of Dennis made me realize of another solution though. To
create another class called Articles which return multiple articles.
The only problem I forsee with this that's it's gonna be a very empty
class.
Then maybe it should not be a class. Maybe a function returning
`Article`\s would be enough. This is not Java, not everything has to be
stuffed into classes.

Ciao,
Marc 'BlackJack' Rintsch
Dec 29 '07 #8
On Dec 29, 1:53 am, Petar <peros...@gmail .comwrote:
Let me explain how I got to this question. I had written een Article
class which handled the articles that I had. On a certain page I
wanted to show all the articles. That got me wondering about what to
do. Should I make a method in my Article.class which returned multiple
articles, or should I just use my current Article.class and fill a
list (with a loop) with articles. The first solution thus meaning
writing another method, the latter method to just use the current
Article.class and call it multiple times.
A class method would be ideal for this if you have no other reason to
create an Articles class:

@classmethod
def all(class_):
ret = []
for a in THE_ARTICLES:
article = class_(a)
ret.append(arti cle)
return ret

Definitely don't use an instance method, which is expected to operate
on one article without making additional Article instances.

Mark 'Blackjack' Rintsch wrote:
Then maybe it should not be a class. Maybe a function returning
`Article`\s would be enough. This is not Java, not everything
has to be stuffed into classes.
True, but this function is logically related to the Article class, so
it's convenient to put them together. Then the user can just "from __
import Article" rather than "from __ import Article, get_all_article s,
what_was_that_o ther_function?" .
Dec 31 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
19304
by: RSH | last post by:
I am really trying to grasp the concept of OOP as it applies to C#. I am looking at trying to set up a simple Employee Class but I am having trouble conceptualizing what this class should look like. I was hoping someone might be able to simply outline what I envision my class to look like: Basically I am envisioning a Class called Employee: I imagine it would have many properties(?) such as:
6
1704
by: RSH | last post by:
I am still trying to grasp the use of real world Objects and how to conceptualize them using a business scenerio. What I have below is an outline that I am wrestling with trying to figure out a class structure:\ Top level Objects: Companies Employees
12
1309
by: RSH | last post by:
I am still trying to grasp the use of real world Objects and how to conceptualize them using a business scenerio. What I have below is an outline that I am wrestling with trying to figure out a class structure:\ Top level Objects: Companies Employees
3
1943
by: Scott Stark | last post by:
Hello, I'm trying to get a better handle on OOP programming principles in VB.NET. Forgive me if this question is sort of basic, but here's what I want to do. I have a collection of Employee objects that I can iterate through relatively easily. I've included code at the bottom of this message. I can pretty easily iterate through my employee objects like so: Dim theEmployees As Employees = New Employees
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.