Last active
January 22, 2020 08:44
-
-
Save z00sts/6531125a698d9d5de9d48933d8a7b08e to your computer and use it in GitHub Desktop.
call_limit.py
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
class CallLimitException(Exception): | |
pass | |
def call_limit(limit=10): | |
def deco(wrapped): | |
nonlocal limit | |
i = 0 | |
def wrapper(*args, **kwargs): | |
nonlocal i | |
i += 1 | |
if i > limit: | |
raise CallLimitException(i) | |
return wrapped(*args, **kwargs) | |
return wrapper | |
return deco | |
@call_limit(2) | |
def f(x): | |
print(f'f({x})') | |
return x | |
for j in range(1, 20): | |
print(f'{j}: {f(j)}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment