1

I am currently working on this code for my homework assignment involving Matrix Implementation:

class MyMatrix(object):
    def __init__(self, n, m, t):
        self.n = n
        self.m = m
        self.t = t

        self.data = []
        for i in range(0, self.n):
            row = []
            for i in range (0, self.m):
                row.append(self.t())
            self.data.append(row)

    def set(self, i, j, v):
        self.data[i][j] = v

    def get(self, i, j):
        self.data[i][j]

    def __str__(self):
        n = self.__class__.__name__ + "({})".format((self.n,self.m))
        for i in range(0, self.n):
            for j in range(0, self.m):
                s += str(self.get(i,j)) + " "
            s += "\n"
        return s

class MySparseMatrix(MyMatrix):

    def __init__(self):
        def __init__(self, n, m, t):
            self.n = n
            self.m = m
            self.t = t
            self.data = {}

    def set(self, i, j, v):
        key = (i, j)
        self.data[key] = v

    def get(self, i,j):
        key = (i, j)
        return self.data.get(key, self.t())

I am trying to print:

tt = MySparseMatrix(int, 2, 2)
tt.set(0,0,11)
tt.set(0,1,5)
tt.set(1,0,2)
print(tt.get(0,1))
print("tt = ", tt)

But it gives me

TypeError: __init__() takes 1 positional argument but 4 were given

Any suggestions on how to fix this error? I am very new to Python.

(Said I needed to add more detail to my question so ignore this: dfjsdfasjdfasdjfasfajfkajsfkajsdfksakfjasdfkjasdfkasdfnasdfbafdbhsdfdfdfskfjdfjsfjsfsjafasjdfaskfgskadfakfaskdfaskfdalsfkasfdkalsdfkasldfksldfkasdfkasdflksdflaksfdlkasdflaksdfkafa)

Kayla Stewart
  • 31
  • 1
  • 4

1 Answers1

0

The problem here is in the __init__ function in the MySparseMatrix; there are two __init__ functions in the definition, meaning that when you create a MySparseMatrix object, it is not calling the __init__ function which accepts arguments.

To fix it, just remove the first __init__ like so:

def __init__(self, n, m, t):
    self.n = n
    self.m = m
    self.t = t
    self.data = {}
lyxal
  • 1,102
  • 1
  • 14
  • 21