cds-202/class/dataclass_tutorial.py
2026-02-26 12:25:16 +01:00

27 lines
466 B
Python

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("...")