I am writing a matrix class and I want to put a method of the same class in the attribute class
class Matrix:
def __init__(
self,
rows: int=None,
columns: int=None,
values: list[int | float | list[int | float]]=None
) -> None:
self.__rows = 3 if not rows else (3 if rows <= 0 else rows)
self.__columns = 3 if not columns else (3 if columns <= 0 else columns)
self.__values = [
[
random.randint(-10, 10)
for _ in range(self.__columns)
]
for _ in range(self.__rows)
] if not values and self.__rows != 1
else [
random.randint(-10, 10)
for _ in range(self.get_columns)
] if self.__rows == 1 else values
self.T = self.__transposition()
def __transposition(self) -> typing.Self:
return Matrix(
self.__columns,
self.__rows,
[
[
self.__values[j][i]
for j in range(self.__rows)
]
for i in range(self.__columns)
]
)
if name == 'main':
a = Matrix(rows=2)
print(a.T)
But there is a depth of recursion
File "Matrix.py", line 25, in __init__
self.T = self.__transposition()
^^^^^^^^^^^^^^^^^^^^^^
File "Matrix.py", line 47, in __transposition
return Matrix(
^^^^^^^
RecursionError: maximum recursion depth exceeded
What can be done? I tried to pass it through the decorator of the static method, but nothing helped.
egorka_angelskiy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4
Instead of evaluating the transpose when the matrix is created, evaluate it only when the property is accessed. You can do that with the @property
decorator. And to avoid recalculating on every access, you can use @functools.cached_property
import typing, random
import functools
class Matrix:
def __init__(
self,
rows: int=None,
columns: int=None,
values: list[int | float | list[int | float]]=None
) -> None:
self.__rows = 3 if not rows else (3 if rows <= 0 else rows)
self.__columns = 3 if not columns else (3 if columns <= 0 else columns)
self.__values = [
[
random.randint(-10, 10)
for _ in range(self.__columns)
]
for _ in range(self.__rows)
] if not values and self.__rows != 1
else [
random.randint(-10, 10)
for _ in range(self.get_columns)
] if self.__rows == 1 else values
# Method renamed and decorator added.
@functools.cached_property
def T(self) -> typing.Self:
return Matrix(
self.__columns,
self.__rows,
[
[
self.__values[j][i]
for j in range(self.__rows)
]
for i in range(self.__columns)
]
)
if __name__ == '__main__':
a = Matrix(rows=2)
print(a.T)