Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # author: Bartlomiej "furas" Burek (https://blog.furas.pl)
- # date: 2020.07.07
- # https://stackoverflow.com/questions/62780283/why-am-i-getting-attributeerror-for-tuple-object
- # Why am i getting AttributeError for tuple object? [closed]
- class Matrix:
- def __init__(self, rows=0, columns=0):
- if rows == 0 and columns == 0:
- self.rows, self.columns = map(int, input().split())
- else:
- self.rows = rows
- self.columns = columns
- self.dimension = (self.rows, self.columns)
- def pack_matrix(self):
- self.value = tuple(tuple(int(i) for i in input().split()) for _ in range(self.rows))
- def __str__(self):
- string = ""
- for row in range(self.rows):
- for col in range(self.columns):
- string = string + str(self.value[row][col]) +" "
- string +="\n"
- return string.strip()
- def __add__(self, other):
- if self.rows == other.rows and self.columns == other.columns:
- new_matrix = Matrix(self.rows, self.columns)
- all_rows = []
- for row in range(self.rows):
- all_cols = []
- for col in range(self.columns):
- all_cols.append(self.value[row][col] + other.value[row][col])
- all_cols = tuple(all_cols)
- all_rows.append(all_cols)
- all_rows = tuple(all_rows)
- #new_matrix.value = tuple(tuple(int(i) for i in row) for row in range(all_rows))
- new_matrix.value = all_rows
- return new_matrix
- else:
- return "ERROR"
- # --- main ---
- m = Matrix(1, 2)
- m.value = ((1,2),)
- n = Matrix(1, 2)
- n.value = ((3,4),)
- print( (m + n).value )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement