I don't really understand the inheritance method of python. I have a class like this, it handles a special matrix.
class PSSM:
matrix = {}
matrixfile = ""
def __init__(self,matrixfile):
self.matrixfile = matrixfile
PSSM.matrix = {}
PSSM.matrixfile = matrixfile
try:
f = open("%s" %matrixfile,"r")
for i in f:
PSSM.matrix[i.split()[0]] = []
for j in i.split():
try:
PSSM.matrix[i.split()[0]].append(float(j))
except ValueError:
continue
f.close()
except IOError:
try:
for i in StringIO(matrixfile):
PSSM.matrix[i.split()[0]] = []
for j in i.split():
try:
PSSM.matrix[i.split()[0]].append(float(j))
except ValueError:
continue
except ValueError:
return
def normalize(self):
newmatrix = self.matrix
for i in range(len(newmatrix["A"])):
suma = 0
for j in newmatrix.keys():
suma += newmatrix[j][i]
for j in newmatrix.keys():
newmatrix[j][i] = newmatrix[j][i]/suma
results = ""
for i in newmatrix.keys():
results += i
for j in newmatrix[i]:
results += "\t% 6.2f" %j
results += "\n"
return results
The program runs fine, but when I try to print out and test it I encountered some unexpected results.
I have a printmatrix
object as well, which print the matrix (adds it into a string and returns the string).
#returns the matrix I used as input
print PSSM.printmatrix(a)
# returns the normalized matrix as expected
print PSSM.normalize(a)
But if I run
print PSSM.printmatrix(a)
again it returns the normalized matrix.
Somehow the matrix
variable inside the class inherits the newmatrix
variable?
How can the matrix
variable chance if I havent changed it anywhere?
I just copyed values form it, and only altered the newmatrix
variable.