473,383 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

is extending an object considered acceptable usage?


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.
Jul 18 '05 #1
2 1171

"mike" <ng************@icewater.org> wrote in message
news:qi*******************@fe06.lga...
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

Jul 18 '05 #2
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.

Jul 18 '05 #3

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

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
106
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the...
2
by: Barry Hynes | last post by:
G'Day folks, Have been working on this problem for quite some time and still no farther ahead. :( Here is my problem...bare with me i am very green :) I have to implement a Safe List,...
1
by: Mark Overstreet | last post by:
Hi, I have several custom collections that inherit from System.Collections.Specialized.NameObjectCollectionBase and I want to serialize and deserialize with the XMLSerializer object. This works...
0
by: Scott Hamlin | last post by:
I have created an ASP.NET application that has a series of TextBox and Label controls inside a DataList. I need to determine what the Label Control is (it will hold the primary key of the DB record...
16
by: Java script Dude | last post by:
Creating a method of object (Object.prototype.classOf ...) is not the correct way because of a (as expected) flaw in IE where DOM Elements does not inherit from Object. As a result it is best to...
13
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){},...
1
by: =?Utf-8?B?VmVua2F0ZXNhbiBT?= | last post by:
Hi, I have a requirement of consuming a connection object returned from a COM component deployed in COM+ application. I have given the need for this requirement end of my query. My component...
8
by: Hussein B | last post by:
Hey, I noted that Python encourage the usage of: -- obj.prop = data x = obj.prop -- to set/get an object's property value. What if I want to run some logic upon setting/getting a property?...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.