I wanna print an all zeros matrix this way:
0 0 0
0 0 0
0 0 0
In order to do that i gotta use the __str__
method. This is what I've gotten so far:
class Matrix:
def __init__(self, m, n):
self.rows = m # rows
self.cols = n # columns
self.matrix = [] # creates an array
for i in range(self.rows):
self.matrix.append([0 for i in range(self.cols)])
def __str__(self):
# string = ""
for element in self.matrix:
return print(*element)
a_matrix = Matrix(3, 3)
print(a_matrix)
But when i run the code, there's an error:
Traceback (most recent call last):
File "C:\Users\DELL USER\Google Drive\Programacion\Negocios\main.py", line 72, in <module>
print(a_matrix)
TypeError: __str__ returned non-string (type NoneType)
0 0 0
Process finished with exit code 1
Note how i'm using return print(*element)
, usually, out of the str method it works just fine but when i use it that way, it stops working. Is there a way i can covert that print to a string so i can get rid of the error?