[Tutor] class question
Steven D'Aprano
steve at pearwood.info
Wed Jan 26 22:48:52 CET 2011
Elwin Estle wrote:
> --- On Wed, 1/26/11, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
> From: Alan Gauld <alan.gauld at btinternet.com>
> Subject: Re: [Tutor] class question
> To: tutor at python.org
> Date: Wednesday, January 26, 2011, 1:10 PM
>>> Is this really a series of different types of casting or a single Workpiece going through a sequence of Actions
>>> each Action having a set of quality data associated?
>
>>> If so it may be that you have a single Workpiece class and a relatively small heirarchy of Actions.
>
> No, there are multiple castings, anywhere from smallish parts you can hold in the palm of your hand, up to engine blocks weighing nearly 2 tons.
Then each such casting has its own set of actions and chain of classes.
There is a process that goes from egg to chick to hen to KFC Chicken
Nuggets, but you wouldn't write a single class to model all of those
things. You would write a separate class for each distinct stage of the
process, and write a process that replaces each data item with the next
one in the series.
I think you are focused too heavily on the fact that it's the same lump
of metal all the way through the pipeline. That's the least important
part of the design -- you can do that with a simple record:
class Lump:
def __init__(self, id, kind):
self.id = id
self.obj = kind()
lumps = [
Lump(1, EngineBlockCasting),
Lump(2, EngineBlockCasting),
Lump(3, WidgetCasting),
Lump(4, MiniCasting),
... ]
and then later on:
lumps[1].obj = EngineBlockWithHolesDrilled()
Although of course you will write functions or methods to move from one
class to another, e.g. drill_holes(lumps, 1), rather than do it all by hand.
--
Steven
More information about the Tutor
mailing list