76 lines
2.4 KiB
Python
76 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Mathematische Vektorenals Klasse
|
|
|
|
__magic_methods__
|
|
"""
|
|
class VectorError(Exception):
|
|
pass
|
|
class VectorTypeError(VectorError):
|
|
pass
|
|
|
|
|
|
# Oberklassen werden automatisch vererbt
|
|
class Vector():
|
|
|
|
def __init__(self, *values):
|
|
"Initialisierung 1 Funktion nach Erzeugen des Objekts"
|
|
self.my_values = values
|
|
def __len__(self): #entspricht len länge in der class
|
|
return len(self.my_values)
|
|
|
|
def __str__(self) -> str: #entspricht str string in der class
|
|
return type(self).__name__ + str(self.my_values)
|
|
|
|
def __del__(self):
|
|
"Destruktor: Letzte Funktion vor Freigabe des Speichers"
|
|
print("Ein Objekt wird freigegeben ({})".format(self.my_values))
|
|
|
|
def get_values(self):
|
|
return self.my_values
|
|
|
|
def test (self, *args):
|
|
# Der erste Weert in tuple ist das Objekt selbst. tuple werden mit * angeben.
|
|
print("Aufruf")
|
|
print(args)
|
|
|
|
def __mul__(self, factor): #entspricht multiplikation * in der class
|
|
multipi = [v * factor for v in self.my_values]
|
|
return type(self)(*multipi) ##Nicht vergessen * und classename
|
|
|
|
def __add__(self, other): #entspricht addition + in der class
|
|
if not isinstance(self, Vector) or not isinstance(other, Vector):
|
|
raise VectorTypeError("Addition nur von Vektoren")
|
|
if len(self) !=len(other):
|
|
raise VectorTypeError("Vektoren ungleicher Laenge")
|
|
summenwert = []
|
|
#zip iteriert ueber alle angegebenen Iterables parallel
|
|
for a, b in zip(self.get_values(), other.get_values()):
|
|
summenwert.append(a + b)
|
|
return type(self)(*summenwert)
|
|
|
|
def info(self):
|
|
return type(self).__name__
|
|
|
|
def poly(self, *args):
|
|
if len(args) == 1:
|
|
if type(args[0]) is str:
|
|
print("Stringverarbeitung")
|
|
elif type(args[0]) is int or type(args[0]):
|
|
print("Intverarbeitung")
|
|
elif len(args) == 2:
|
|
self.poly_2param(*args)
|
|
elif len(args) == 0:
|
|
Vector.poly_0param(self, *args)
|
|
|
|
|
|
class Vector2(Vector):
|
|
def __init__(self, val1, val2):
|
|
super().__init__(val1, val2)
|
|
|
|
def __add__(self, other):
|
|
sum = [d + other for d in self.my_values]
|
|
# return super(self).__add__(Vector2.sum)
|
|
|
|
def __mul__(self, factor):
|
|
return super().__mul__(factor) |