[Tutor] Dictionaries versus classes

Alan Gauld alan.gauld at freenet.co.uk
Mon Feb 6 23:15:13 CET 2006


> In the end my program will have to create a printed representation of a
> certain number of "things" ... 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 ...

Things with attributes and varying behaviour is almost a definition of 
objects.
There may be a clue there...

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

Dynamic rules are always tricky to get right however there are a couple of
design patterns that can be adapted in the form of the visitor and strategy
patterns. If your visitor pattern uses a strategy you get a customisable set
of behaviours. But it depends a little on just how complex the rules get...

> 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

Almost never. If its only one or two functions maybe but the minute it
gets beyond a couple a class will almost always be better. The overhead
of a Python class definition is tiny compared to the benefits.

> Where could I read up something about relative advantages / disadvantages
> of each approach ?

General OO design books are the only place I suspect.
Try it both ways and learn from experience is the other alternative.

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list