[Tutor] Should I create a Class for this?

D-Man dsh8290@rit.edu
Tue, 12 Dec 2000 23:48:53 -0500


Here's how a class might be useful:

You create a class called Employee.  It's constructor takes some argument, like
maybe a row in a table.  The ctor (short for constructor) will then interpret
the data in the row and initialize some instance variables, such as hours,
months, salary, etc.  The functions would be members of the class.  Thus they
have access to instance variables, which are shared by all the functions of the
same object.  (note I said object, not class becuase they are instance, not
class variables).  Then the functions, such as the parttimepercent function you
gave would not take any arguments.  It already has the data it needs in the
class.

ex:

# being cheap in the ctor here and making the client parse the database stuff
class Employee :

	def __init__( self , hours , months ) :
		if ( hours == 0 ) :	
			# should this be an error instead of a default?
			# it's up to what you want the calculation to mean
			self.hours = 40
		else :
			self.hours = hours

		if ( months == 0 ) :
			# same here about the error
			self.months = 12
		else :
			self.months = days

	def part_time_percent( self ) :
		return  (self.hours * self.months ) / 480
# end class Employee

# this is the main()
emp = Employee( 40 , 5 ) # ok, he only worked 1 week :-)
print emp.part_time_percent()


I will also point out that Python doesn't enforce encapsulation.  However, I
feel that encapsulation is very important and write code as if it did.  C++ and
Java support it through the 'private' keyword.  Eiffel won't let a client assign
to a data member even if you try to make it public.

Does this help at all?

-D

On Tue, 12 Dec 2000 18:38:55 Brad Chandler wrote:
 | ----- Original Message -----
 | From: "Remco Gerlich" <scarblac@pino.selwerd.nl>
 | 
 | 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
 | 
 |