is extending an object considered acceptable usage?

Discussion in 'Python' started by mike, Jan 25, 2005.

  1. mike

    mike Guest

    i have an Item which belongs to a Category, so Item has:

    - item.categoryId, the database primary key of its Category

    - item.category, a reference to its Category. this null unless i need a
    reference from item to its Category object, in which case i call
    setCategory(category)

    sometimes i want a list of categories, and from each i want to be able
    to access a list of its items. in this case is it considered acceptable
    to just create a list of those items and assign it as a property of
    their category? eg:

    category.items = listOfItems

    this packages everything up into a hierarchy and is more convenient to
    use, especially in Cheetah templates, but requries modifying the
    structure of the object, which bothers me (probably for some
    subconscious java-related reason).

    the alternative might be to create a dictionary that keys the lists of
    items on their category:

    items = {}
    items[category.id] = listOfItems

    this feels less "controversial" to me, but requires extra objects and
    house-keeping.


    thanks - just curious if there were arguments one way or the other.
     
    mike, Jan 25, 2005
    #1
    1. Advertising

  2. mike

    Terry Reedy Guest

    "mike" <> wrote in message
    news:qijJd.14679$...
    > sometimes i want a list of categories, and from each i want to be able to
    > access a list of its items. in this case is it considered acceptable to
    > just create a list of those items and assign it as a property of their
    > category? eg:


    To me, Python is about being what it is, and not any other language. It is
    about writing correct readable algorithms. It is about enjoying
    programming. It is about programmers being responsible for their own code.

    I hope you find this more helpful than any yes or no could be.

    Terry J. Reedy
     
    Terry Reedy, Jan 25, 2005
    #2
    1. Advertising

  3. mike

    Larry Bates Guest

    When I want to do what I think you are asking, I create
    an iterator in that returns a category item each time
    the .next method is called. That way you can write.

    ITEM=item(<args>)
    ..
    ..
    ..
    for CATEGORY in ITEM:
    <do anything>


    in my item class:

    class item:
    def __init__(self, <other args>):
    self.CATEGORIES=[]
    self.next_index=0 # Index point for next method

    def __iter__(self):
    return self

    def next(self):
    #
    # Try to get the next route
    #
    try: CATEGORY=self.CATEGORIES[self.next_index]
    except:
    self.next_index=0
    raise StopIteration
    #
    # Increment the index pointer for the next call
    #
    self.next_index+=1
    return CATEGORY

    I had one project where these were nested 5-6 deep
    and the resultant code reads beautifully.

    Larry Bates


    mike wrote:
    >
    > i have an Item which belongs to a Category, so Item has:
    >
    > - item.categoryId, the database primary key of its Category
    >
    > - item.category, a reference to its Category. this null unless i need a
    > reference from item to its Category object, in which case i call
    > setCategory(category)
    >
    > sometimes i want a list of categories, and from each i want to be able
    > to access a list of its items. in this case is it considered acceptable
    > to just create a list of those items and assign it as a property of
    > their category? eg:
    >
    > category.items = listOfItems
    >
    > this packages everything up into a hierarchy and is more convenient to
    > use, especially in Cheetah templates, but requries modifying the
    > structure of the object, which bothers me (probably for some
    > subconscious java-related reason).
    >
    > the alternative might be to create a dictionary that keys the lists of
    > items on their category:
    >
    > items = {}
    > items[category.id] = listOfItems
    >
    > this feels less "controversial" to me, but requires extra objects and
    > house-keeping.
    >
    >
    > thanks - just curious if there were arguments one way or the other.
     
    Larry Bates, Jan 28, 2005
    #3
    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. valentin tihomirov

    Is this trick with reset acceptable?

    valentin tihomirov, Apr 11, 2004, in forum: VHDL
    Replies:
    6
    Views:
    582
    Jim Lewis
    Apr 14, 2004
  2. .Net Sports
    Replies:
    2
    Views:
    1,055
    Craig Deelsnyder
    May 3, 2005
  3. steve

    acceptable way to program

    steve, Dec 31, 2004, in forum: Java
    Replies:
    34
    Views:
    1,054
    Haximus
    Feb 20, 2005
  4. (Pete Cresswell)
    Replies:
    8
    Views:
    485
    Duende
    Dec 23, 2004
  5. Kay Schluehr
    Replies:
    7
    Views:
    298
    Terry Reedy
    May 17, 2008
Loading...

Share This Page