[Tutor] dictionary dispatch for object instance attributes question

Jeff Shannon jeffshannon at gmail.com
Wed Feb 16 22:09:28 CET 2005


On Tue, 15 Feb 2005 23:48:31 -0500, Brian van den Broek
<bvande at po-box.mcgill.ca> wrote:
> Jeff Shannon said unto the world upon 2005-02-15 21:20:
> > On Tue, 15 Feb 2005 17:19:37 -0500, Brian van den Broek
> > <bvande at po-box.mcgill.ca> wrote:
> >
> > For starters, I've made metadata a class attribute rather than an
> > unconnected dictionary.  This seems conceptually nicer to me.
> 
> The problem is that my Node instance live in a TP_file class instance,
> and the way my code is now, the TP_file instance also needs to see the
> metadata dict. There are a few tags, which if present in any Node of
> the file make me want to treat the entire file a bit differently. (Of
> course, here is the place where my novice-designer status is most
> likely to be bitting me.) So, that's why I have it as a module level
> object, rather than within a class. (I do, however, see your point
> about it being neater.)

Okay, that makes sense.  You have two different classes (the TP_file
class and the Node class) that need access to the same information, so
yes, having it at the module level lets them share it more
effectively.  (Alternately, since it sounds like the TP_file class is
where all of the Node instances are created, you *could* decide that
the metadata belongs as part of the TP_file, which would then actively
share it with Node... but what you've got sounds like a very
reasonable plan, so at this point I wouldn't worry about it.)

> > In addition, update() can now modify several attributes at once, at
> > the cost of a bit of extra parsing up front.
> 
> The metadata all occurs one element to a line in my original file.
> [...] Maybe I'm still missing a better way, but as I am processing
> line by line, each line with one element, I don't see how to use this
> cool looking multiple elements at once approach. 

Yes, if you know that you will only have one header per line, then
it's reasonable to process them one line at a time.  You could
alternatively have the TP_file gather all the header lines for a given
node into a list, and then process that list to create the Node
instance, but given the specifics of your case you probably wouldn't
gain anything over your current approach by doing so.

This is what makes programming so interesting -- there's so many
different choices possible, and which one is best depends on a large
number of factors.  When writing a program for some task, the best
design for a particular set of circumstances may be completely
different than the best design for a somewhat different particular set
of circumstances -- and the best design for general usage is probably
an altogether different thing still.

Good luck!

Jeff Shannon


More information about the Tutor mailing list