new
This commit is contained in:
1 parent
1934fdfc63
commit
f731c70a18
1 file changed
+26
@@ -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
|
||||
Reference in new issue
Block a user