I have a function that returns a special list that has been prepared according to the function's options:
def Matrix(
numberOfColumns = 3,
numberOfRows = 3,
element = 0.0
):
matrix = []
for column in range(numberOfColumns):
matrix.append([element] * numberOfRows)
return(matrix)
I want to create a new class based on the existing class list
and add to it various methods and various arguments in order to create a special list, much as in the way shown in the function above. I can create this new class and add methods to it in a way such as the following:
class Matrix(list):
def __init__(self, *args):
list.__init__(self, *args)
def printout(self):
return("PRINTOUT")
I am not sure how to add new arguments to the new class. The class list
accepts one positional argument and I'm not sure how to add more to the class in a sensible way.
Specifically, I would likely want to be able to create an instance of the new class in a way such as the following:
a = Matrix(numberOfColumns = 3, numberOfRows = 2)
I have the class set up in the following way:
class Matrix(list):
def __init__(
self,
numberOfColumns = 3,
numberOfRows = 3,
element = 0.0,
*args
):
self.numberOfColumns = numberOfColumns
self.numberOfRows = numberOfRows
super().__init__(self, *args)
def printout(self):
return("PRINTOUT")
When I instantiate it in the following way:
a = Matrix(numberOfColumns = 3)
I encounter the following error:
TypeError: super() takes at least 1 argument (0 given)
When I instantiate it in the following way:
a = Matrix("1", numberOfColumns = 3)
I encounter the following error:
TypeError: __init__() got multiple values for keyword argument 'numberOfColumns'
Where am I going wrong?