class Matrix:
rowStorer = {}
generalPurposeList = []
def __init__(self,row,column):
self.row = row
self.column = column #To store away for a later method that displays statistics about matrice
for i in range(row):
for j in range(column):
numb = int(input('Enter A{}{}: '.format(i+1,j+1))) #i+1 and j+1 so I do not end up with 0 as starting value
self.generalPurposeList.append(numb)
self.rowStorer.setdefault(i+1,self.generalPurposeList)
self.generalPurposeList.clear()
def __str__(self):
megaMatrix = ''
for i in self.rowStorer:
megaMatrix += str(self.rowStorer[i])+'\n'
return megaMatrix
testRide = Matrix(2,3)
print(testRide.__str__())
I am a beginner to Python classes and am trying to make a Matrix class as practice. The user specifies the number of rows and columns when creating a class instance.
Assuming row as m and column as n, an entry for any row m is taken n times using the input() in the for-loop. This number is then added to a list which is meant to contain that specific row's elements. The current row number is added to a dictionary as a key with the value being the list containing that specific row's elements
I then clear this list after its added to the dictionary as a key, to clean it up and ready it for the next row's elements. However, when I run the program, it seems to CLEAR the list first and then add it to the dictionary?
Im confused, what did I do wrong? Shouldn't it clear AFTER its been added to the dict as per the code?