Skip to content

Instantly share code, notes, and snippets.

@curegit
Created March 11, 2024 05:57
Show Gist options
  • Save curegit/ccd551950d8dfa5be7ddfdff089f5dc3 to your computer and use it in GitHub Desktop.
Save curegit/ccd551950d8dfa5be7ddfdff089f5dc3 to your computer and use it in GitHub Desktop.
高々一回実行される副作用を定義するデコレータ
import functools
def once(func):
fst = True
result = None
@functools.wraps(func)
def wrapper(*args, **kwargs):
nonlocal fst, result
if fst:
fst = False
result = func(*args, **kwargs)
return result
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment