Created
March 11, 2024 05:57
-
-
Save curegit/ccd551950d8dfa5be7ddfdff089f5dc3 to your computer and use it in GitHub Desktop.
高々一回実行される副作用を定義するデコレータ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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