[Tutor] Class inheiritance question

mbc2@netdoor.com mbc2@netdoor.com
Tue, 2 Jan 2001 12:18:25 -0600 (CST)

Ok, I ran into some problems trying to redefine a variable set in the
constructor. The problem was that I not only had variables that
initialized my class from the database row passed to it, but I had also
set variables in the constructor using some of the class's methods. For

class Position:
	def __init__ (self, dbrow):
		self.calenrate = .1 #this is just a constant
		#I decided to pass a dictionary to the class using the db
		#field name as the key, in order to make the program a
		#little more flexible.
		self.CURYRSAL = dbrow.get("curyrsal","unknown")
		self.basesalary = self.Basesalary()

	def Basesalary (self):
		x = self.CURYRSAL*(1+self.calenrate)
		return x

I did this because there were other methods of the class which needed to
use the basesalary number. I found that even if I redefined CURYRSAL in
the sub class, the Basesalary method still used the old figure because it
was being called in the constructor. For example:

class Positionrlgn(Position):
	def __init__(self,dbrow):
		Position.__init__(self,dbrow) #Basesalary gets called now
		self.CURYRSAL = 100000 #Redefined CURYRSAL but it doesn't
					#matter because basesalary has
					#already been set.

This caused me to think that it was probably best to only put initializing
data into the constructor and not to call any of the methods in the
contructor. So I rewrote my class that way. The problem I ran into (which
is the same problem that caused me to call those methods in the
constructor in the first place) is that I cannot call a method to set a
varialbe.  For example:

def Basesalary (self):
	x = self.CURYRSAL*(1+self.calenrate)
	return x

basesalary = Basesalary() #Type Error: expected 1 argument, got 0
basesalary = Basesalary(self) #Name Error: there is no variable named self
basesalary = self.Basealary() #Name Error: there is no variable named self
basesalary = Position.Basesalary() #Name Error: there is no variable named

So what I ended up doing was setting basesalary = self.Basesalary inside
each function that needed it. That works but results in those functions
being called many times, which has slowed my program down alot.

Am I doing this correctly? Is there a more efficient way. I don't want to
call Basesalary() in the constructor, but I don't want to call it multiple
times either.