OOP: How to implement listing al 'Employees'.

Discussion in 'Python' started by Petar, Dec 28, 2007.

  1. Petar

    Petar Guest

    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.
     
    Petar, Dec 28, 2007
    #1
    1. Advertising

  2. 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.
     
    Bjoern Schliessmann, Dec 28, 2007
    #2
    1. Advertising

  3. Petar

    Petar Guest

    On 28 dec, 13:40, Bjoern Schliessmann <usenet-
    > wrote:
    > 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)?
     
    Petar, Dec 28, 2007
    #3
  4. 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...
     
    Bruno Desthuilliers, Dec 28, 2007
    #4
  5. Petar a écrit :
    > On 28 dec, 13:40, Bjoern Schliessmann <usenet-
    > > wrote:
    >
    >>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 YourDomainClassNameHere 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...
     
    Bruno Desthuilliers, Dec 28, 2007
    #5
  6. Petar a écrit :
    (snip)
    > should a class always
    > reference only on 'item'?


    ???

    I'm afraid I didn't get this part of the question.
     
    Bruno Desthuilliers, Dec 28, 2007
    #6
  7. On Fri, 28 Dec 2007 04:05:59 -0800 (PST), Petar <>
    declaimed the following in comp.lang.python:

    > 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.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

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Dec 28, 2007
    #7
  8. Petar

    Petar Guest

    On 28 dec, 19:42, Dennis Lee Bieber <> wrote:
    > On Fri, 28 Dec 2007 04:05:59 -0800 (PST), Petar <>
    > declaimed the following in comp.lang.python:
    >
    > > 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.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
    >                    
    >                 HTTP://wlfraed.home.netcom.com/
    >         (Bestiaria Support Staff:               )
    >                 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.
     
    Petar, Dec 29, 2007
    #8
  9. 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
     
    Marc 'BlackJack' Rintsch, Dec 29, 2007
    #9
  10. Petar

    Mike Orr Guest

    On Dec 29, 1:53 am, Petar <> wrote:

    > 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(article)
    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_articles,
    what_was_that_other_function?".
     
    Mike Orr, Dec 31, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SklNLkgu?=

    Q: login for employees in internet

    =?Utf-8?B?SklNLkgu?=, Mar 10, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    345
    Scott Allen
    Mar 10, 2005
  2. John
    Replies:
    2
    Views:
    1,367
    llewelly
    Sep 4, 2003
  3. Shane Beasley
    Replies:
    2
    Views:
    398
    Govindan
    Sep 9, 2003
  4. kr0
    Replies:
    2
    Views:
    513
    Adam Maass
    Jun 12, 2006
  5. Steven Arnold
    Replies:
    0
    Views:
    296
    Steven Arnold
    May 19, 2005
Loading...

Share This Page