Flyttet python-kurset ut av samlerepoet

This commit is contained in:
2025-03-04 15:18:01 +01:00
parent c9cc068203
commit f9eb912b6a
38 changed files with 3 additions and 1164 deletions

View File

@@ -0,0 +1,32 @@
"""
Skippable if out of time, hot to define ones own objects.
"""
from __future__ import annotations
class MyVector(list):
a = 0
def __init__(self, dim: int, data: list):
if dim != len(data):
raise ValueError("Wrong dimensions in the vector!!!")
self.dim = dim
self.data = data
def __add__(self, other: MyVector):
if self.dim != other.dim:
return None
else:
return MyVector(self.dim, [a + b for a, b in zip(self.data, other.data)])
def __repr__(self):
return "A vector of dimension {} with data:\n{}".format(self.dim, self.data)
a = MyVector(3, [1, 2, 3])
b = MyVector(3, [-1, -4, 8])
print(a)
print(b)
print(a + b)