-1

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.

taskinoor
  • 45,586
  • 12
  • 116
  • 142
Gábor Erdős
  • 3,599
  • 4
  • 24
  • 56

1 Answers1

2
class PSSM:
    matrix = {}
    matrixfile = ""

Matrix and matrixfile are not bound to an instance but to the class itself. So all instances of that class share it. Move these to the __init__ function:

def __init__(self,matrixfile):
  self.matrix = {}
  self.matrixfile = matrixfile

Same goes for PSSM.matrix, change it to self.matrix

Also working with OO you should create an instance of PSSM and use that.

myMatrixFile = "bla"
myPssm = PSSM(myMatrixFile)

print myPssm.printmatrix() #returns the matrix I used as input
print myPssm.normalize() # returns the normalized matrix as expected
RvdK
  • 19,580
  • 4
  • 64
  • 107
  • I changed the location of "matrix = {}" but the problem is still there. a = PSSM("locationofmatrix") print PSSM.printmatrix(a), print PSSM.normalize(a), print PSSM.printmatrix(a), The second print still prints the normalized insted of the original – Gábor Erdős Jun 02 '15 at 11:37