* 3.9_12 add eq, ne

This commit is contained in:
Dmitry Belyaev 2024-04-17 16:02:02 +03:00
parent 102e8ce6bd
commit b34a6842aa

View File

@ -67,6 +67,13 @@ P.S. В программе нужно объявить только класс.
---
>>> Matrix(2, 2) - 2
Matrix([[-2, -2], [-2, -2]])
>>> Matrix(2, 2) == Matrix([[0, 0], [0, 0]])
True
>>> Matrix(2, 2, 2) == [[2, 2], [2, 2]]
True
>>> Matrix(2, 2, 2) == [[2, -2], [2, 2]]
False
"""
from functools import singledispatchmethod
@ -146,18 +153,6 @@ class Matrix:
new_row.append(value)
self.data.append(new_row)
def __repr__(self):
return f"{self.__class__.__name__}({self.data!r})"
def __len__(self):
return self.rows
def __iter__(self):
return (
(self.cells[row][col].data for col in range(self.cols))
for row in range(self.rows)
)
def validate_index(self, index):
if not isinstance(index, tuple):
raise IndexError("недопустимые значения индексов")
@ -204,6 +199,28 @@ class Matrix:
)
]
def __len__(self):
return self.rows
def __iter__(self):
return (
(self.cells[row][col].data for col in range(self.cols))
for row in range(self.rows)
)
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.data == other.data
if isinstance(other, list):
return self.data == other
return NotImplemented
def __ne__(self, other):
return not self == other
def __repr__(self):
return f"{self.__class__.__name__}({self.data!r})"
def tests():
# 10x = https://stepik.org/lesson/701994/step/12?discussion=5990560&unit=702095