diff --git a/aufgaben/einstellung/Pr_5/ha/nureinmal.py b/aufgaben/einstellung/Pr_5/ha/nureinmal.py new file mode 100644 index 0000000..8a6b40c --- /dev/null +++ b/aufgaben/einstellung/Pr_5/ha/nureinmal.py @@ -0,0 +1,26 @@ +import functools + +def run_once(func): + already_run = False #Wurde die Funktion schon ausgeführt? + + @functools.wraps(func) + def wrapper(*args, **kwargs): + nonlocal already_run # Greife auf die äußere Variable zu; UnboundLocalError + if already_run: + print("Already executed") + return None + already_run = True + return func(*args, **kwargs) + + return wrapper + + +# --- Test --- +@run_once +def hallo(name): + print(f"Hallo, {name}!") + + +hallo("Zlata") # 1. Aufruf -> Ausgabe: Hallo, Zlata! +hallo("Patrik") # 2. Aufruf -> Ausgabe: Already executed +hallo("Zlata") # 3. Aufruf -> Ausgabe: Already executed \ No newline at end of file