[Tutor] Should I create a Class for this?

Remco Gerlich scarblac@pino.selwerd.nl
Tue, 12 Dec 2000 20:02:01 +0100


On Tue, Dec 12, 2000 at 11:33:14AM -0600, Brad Chandler wrote:
> Where I work, we have a report which performs a number of calculations on
> each employee to determine the exact amounts of his salary and fringe
> benefits.  I've put all of these calculations into a python file, defining
> each as a separate function.  There are approximately 67 functions and about
> 50 constant variables that I've defined at the top of the file.
> 
> What is the best way to organize these functions so that I can easily reuse
> them in different programs?  Should I create a class and put them in it.
> I've never created a class before so I'm not exactly sure how to go about
> it.  As it is right now, I can just import the file and access the
> functions.  What would be the advantage of creating a class for this?

It depends, it's not easy to tell what the best way is, from this.

When you code in classes, the *data* comes central. If all those functions
act on the same data (say, an employee number and his base salary), it may
be easier to make a class with that data, and add the functions as methods.
A class is a representation of a block of data that should be seen as one
piece, and the things you can do with that data.

But if the functions all do unrelated things, there is no reason to put
them in a class.

And above all, if it works now, there is probably no reason to change it
into OO for the sake of OO :).

What kind of functions do you have? Do you feel the way it works now
is logical?

-- 
Remco Gerlich