I've written a class called Matrix
.
class Matrix:
# Initialize a matrix
def __init__(self, rows: int = 1, columns: int = 1, data: list = []):
self.data = data
self.rows = rows
self.columns = columns
I'm trying to change the data in only one instance of Matrix
, but it changes the data in both instances.
from matrix import Matrix
m1 = Matrix()
m2 = Matrix()
for i in range(5):
m1.data.append(i)
print(m1.data)
print(m2.data)
# expected result:
# [0, 1, 2, 3, 4]
# []
# actual result:
# [0, 1, 2, 3, 4]
# [0, 1, 2, 3, 4]
Edit:
Changed the Matrix class to this and I'm getting the desired behaviour:
class Matrix:
# Initialize a matrix
def __init__(self, rows: int = 1, columns: int = 1, data: list = []):
if data:
self.data = data
else:
self.data = []
self.rows = rows
self.columns = columns