Donnerstagabend

This commit is contained in:
2022-11-24 17:59:17 +01:00
parent 20066fe48d
commit aea7c693d6
7 changed files with 100 additions and 11 deletions

Binary file not shown.

7
Donnerstag/check_zip.py Normal file
View File

@@ -0,0 +1,7 @@
it1 = range(5)
it2 = range(100,105)
for a, b in zip(it1, it2):
print(a)
print('-' * 20)
print(b)

21
Donnerstag/decorators.py Normal file
View File

@@ -0,0 +1,21 @@
#!/usr/bin/env python3
def counter(func):
def counting(*args, **kwargs):
counting.callcount += 1
return func(*args, **kwargs)
counting.callcount = 0
return counting
def get_counter(func):
try:
return func.callcount
except:
return None
def reset_counter(func):
try:
func.callcount
func.callcount = 0
except:
return None

14
Donnerstag/fakultät.py Normal file
View File

@@ -0,0 +1,14 @@
#!/usr/bin/env python3
from decorators import counter, get_counter
@counter
def fak(n):
if type(n) is not int or n < 0:
raise TypeError("Illegal type")
if n == 0:
return 1
return n * fak(n-1)
print("6! =", fak(6))
print("Anzahl Aufrufe:", get_counter(fak))

View File

@@ -1,10 +1,14 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from vectors import Vector from vectors import Vector, Vector2
v1 = Vector(1, 2, 3, ) v1 = Vector(1, 2, 3)
v2 = Vector(4,0,1) v2 = Vector(4,0,1)
print(v1.get_values()) v3 = v1 * 3
del v2 #v4 = v1 + v2 #.addition(v2)
print("Jetzt wird v2 freigebe ") #print(v3.get_values())
print(str(v1))
#print(v2) print(v3)
#print(v4.get_values())
v5 = Vector2(1, 3)
print(v5 + 3)
print()

View File

@@ -1,16 +1,31 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
""" """
Mathematische Vektorenals Klasse Mathematische Vektorenals Klasse
__magic_methods__
""" """
class VectorError(Exception):
pass
class VectorTypeError(VectorError):
pass
# Oberklassen werden automatisch vererbt # Oberklassen werden automatisch vererbt
class Vector(): class Vector():
values = ()
def __init__(self, *values): def __init__(self, *values):
"Initialisierung 1 Funktion nach Erzeugen des Objekts" "Initialisierung 1 Funktion nach Erzeugen des Objekts"
self.my_values = values self.my_values = values
def __del__(self): 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" "Destruktor: Letzte Funktion vor Freigabe des Speichers"
print("Ein Objekt wird freigegeben ({})".format(self.my_values)) print("Ein Objekt wird freigegeben ({})".format(self.my_values))
def get_values(self): def get_values(self):
return self.my_values return self.my_values
@@ -19,6 +34,24 @@ class Vector():
print("Aufruf") print("Aufruf")
print(args) 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): def poly(self, *args):
if len(args) == 1: if len(args) == 1:
if type(args[0]) is str: if type(args[0]) is str:
@@ -29,5 +62,15 @@ class Vector():
self.poly_2param(*args) self.poly_2param(*args)
elif len(args) == 0: elif len(args) == 0:
Vector.poly_0param(self, *args) 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)