Unification of Methods and Functions

Greg Ewing greg at cosc.canterbury.ac.nz
Mon May 17 00:40:00 EDT 2004


David MacQuigg wrote:
> James has been telling me it is terribly wrong to keep data as
> attributes of classes

There's nothing inherently wrong with that, as long as
you're certain you will only ever want one instance of that
piece of data.

In my chip example, you might want to keep a count of the
total number of transistors in your chip as a class variable
of the Transistor class. But that only works if your program
only ever deals with one chip at a time. If there can be more
than one chip, then you'll need a Chip class whose instances
either keep a count of Transistors contained in that chip,
or have a method that can compute the count when needed.

Even if you think you will only deal with one chip at a
time, it would be good design practice to plan on having
a Chip class anyway, in case the requirements change. The
amount of work involved is about the same either way, and
the design which avoids class variables is more flexible.

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg




More information about the Python-list mailing list