[Tutor] Should I create a Class for this?

Brad Chandler mbc2@netdoor.com
Tue, 12 Dec 2000 17:38:55 -0600


----- Original Message -----
From: "Remco Gerlich" <scarblac@pino.selwerd.nl>

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

Well, the data comes from a database. Each row is a separate position and
contains around 30 fields.  The functions are for calculations such as
retirement, social security, life insurance, etc...  Each takes fields from
the database as arguments and returns a number. At the end, the result of
each function is added together to get the total fringe benefits.  The same
fields are used for multiple functions.  I guess I would see the position as
the object.

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

I'll include an example below.  I've read most of a book on C++ which
describes OO and I've read the section of the Python Tutorial which
describes classes.  I've just never made the leap from theory to practice
and I'm having trouble grasping the idea.  I haven't had that "A ha" moment
yet.

I guess I'm just curious about how to implement these calculations now that
they're written.  I think that I will have to feed a row of data from the
database to a function and have it return the fringe benefits amount to me.
Typically, I'll only need that one number returned, but I wanted to make it
as flexible as possible so that if I needed to access one of the other
functions directly, I would be able to do so.  Is this the proper place for
a class?

Example functions:

#part-time percent
def parttimepercent (hours,months):
    if hours==0:
        h=40
    else:
        h=hours

    if months==0:
        m=12
    else:
        m=months

    return (h*m)/480

#Social Security
def Socialsec (basesalary, ssrate, ssmax):
    if basesalary<=ssmax:
        x=round((basesalary*ssrate),2)
    else:
        x=ssmax*ssrate

    return x