From b34a6842aa9ba64e814cea446b60754587e29867 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 17 Apr 2024 16:02:02 +0300 Subject: [PATCH] * 3.9_12 add eq, ne --- mod_oop/3.9_12_matrix.py | 41 ++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/mod_oop/3.9_12_matrix.py b/mod_oop/3.9_12_matrix.py index 1d0cc63..95cac26 100644 --- a/mod_oop/3.9_12_matrix.py +++ b/mod_oop/3.9_12_matrix.py @@ -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