[Tutor] Dictionaries versus classes
Kent Johnson
kent37 at tds.net
Mon Feb 6 20:11:26 CET 2006
Michele Alzetta wrote:
> In the end my program will have to create a printed representation of a
> certain number of "things" which are characterized by each having a time
> attribute (moment when they start, moment when they end); each "thing"
> will have to be filled in by a certain content, chosen from a user
> defined list of possibilities; however each thing will also have to have
> a series of other attributes ... which will change during the process of
> assigning content (for instance: once content 'A' is assigned to
> foo, bar immediately following may well change some attribute ... BUT
> the effect of content A on foo and bar may well be different from the
> effect of content B. Naturally if content A is present in a certain
> thing it can't be present in a contemporaneaous one... sounds a mess ?
> It is ... also, the rules by which A and B influence the content will
> have to be modifiable by the user.)
>
> At the moment I am collecting user input into a dictionary of lists. The
> question is: do you believe it would be better to code a series of
> functions to interact with my dictionary and keep data, attributes etc.
> in lists therein, or would it be more efficient to create an appointment
> class, create a new instance of each class for each appointment, and
> eventually store each instance in a dictionary ?
I don't understand your requirements, but I would think the choice would
be between a list of dicts and a list of custom classes. I don't see how
the dict of lists would work. Do you have parallel lists, one for each
attribute? IMO that is generally a bad design, the attributes should be
grouped somehow.
It sounds like an appointment class might help, it would give you a
place to put the code that operates on appointments and give you more
convenient access to attributes. Probably a Rule class would help too.
You could try using a dict to represent an appointment - the simplest
thing that could possibly work. If you have to create a bunch of
functions that operate on a single appointment at a time, then switch to
a class-based implementation.
Kent
More information about the Tutor
mailing list