From 0ea404ac019ba39ffe4da921ac764c91324c2775 Mon Sep 17 00:00:00 2001 From: git-sandro Date: Thu, 26 Feb 2026 12:25:16 +0100 Subject: [PATCH] feat: class tutorials --- class/class_introduction.py | 10 ++++++++++ class/dataclass_tutorial.py | 27 +++++++++++++++++++++++++++ class/konto.py | 21 +++++++++++++++++++++ class/repr_tutorial.py | 10 ++++++++++ class/str_tutorial.py | 15 +++++++++++++++ 5 files changed, 83 insertions(+) create mode 100644 class/class_introduction.py create mode 100644 class/dataclass_tutorial.py create mode 100644 class/konto.py create mode 100644 class/repr_tutorial.py create mode 100644 class/str_tutorial.py diff --git a/class/class_introduction.py b/class/class_introduction.py new file mode 100644 index 0000000..048c893 --- /dev/null +++ b/class/class_introduction.py @@ -0,0 +1,10 @@ + +class Konto(): + pass + +k1 = Konto() +k2 = Konto() + +print(k1) +print(k2) +print(k1 is k2) diff --git a/class/dataclass_tutorial.py b/class/dataclass_tutorial.py new file mode 100644 index 0000000..c467879 --- /dev/null +++ b/class/dataclass_tutorial.py @@ -0,0 +1,27 @@ +from dataclasses import dataclass + +@dataclass +class Student: + name: str + nr: int + sem: int + +@dataclass +class Rechteck: + breite: float + hoehe: float + + def flaeche(self) -> float: + return self.breite * self.hoehe + +# Validierung +@dataclass +class Produkt: + name: str + preis: float + + def __post_init__(self): + if not self.name: + raise ValueError("...") + if self.preis < 0: + raise ValueError("...") \ No newline at end of file diff --git a/class/konto.py b/class/konto.py new file mode 100644 index 0000000..efb36fc --- /dev/null +++ b/class/konto.py @@ -0,0 +1,21 @@ +class Konto(): + def __init__(self, inhaber, saldo): + if not inhaber: + raise ValueError("Inhaber darf nicht leer sein") + if saldo < 0: + raise ValueError("Saldo darf nicht negativ sein") + self.inhaber = inhaber + self.saldo = saldo + + def einzahlen(self, betrag): + self.saldo += betrag + + def abheben(self, betrag): + self.saldo -= betrag + +k = Konto("Alice", 100) +print(k.saldo) +k.einzahlen(25) +print(k.saldo) +k.abheben(45) +print(k.saldo) \ No newline at end of file diff --git a/class/repr_tutorial.py b/class/repr_tutorial.py new file mode 100644 index 0000000..902944c --- /dev/null +++ b/class/repr_tutorial.py @@ -0,0 +1,10 @@ +class Konto: + def __init__ ( self , inhaber , saldo ) : + self . inhaber = inhaber + self . saldo = saldo + + def __repr__ ( self ) : + return f"Konto(inhaber = {self.inhaber!r}, saldo = {self.saldo!r})" + +k = Konto ( " Alice " , 100.0) +print ( k ) # Konto ( inhaber = Alice , saldo =100.0) \ No newline at end of file diff --git a/class/str_tutorial.py b/class/str_tutorial.py new file mode 100644 index 0000000..fab13a3 --- /dev/null +++ b/class/str_tutorial.py @@ -0,0 +1,15 @@ +class Konto: + def __init__ (self, inhaber, saldo): + self.inhaber = inhaber + self.saldo = saldo + + def __repr__(self): + return f"Konto(inhaber = {self.inhaber!r}, saldo = {self.saldo!r})" + + def __str__(self): + return f"Konto von {self.inhaber}: CHF {self.saldo:.2f}" + +k = Konto ( " Alice " , 100.0) +print(str(k)) # __str__ +print(repr(k)) # __repr__ +print(k) # bevorzugt __str__ , sonst __repr__ \ No newline at end of file