Last active
April 5, 2021 21:46
-
-
Save vsaraph/ad9aae917bc540749066f01cc341229b to your computer and use it in GitHub Desktop.
Python decorator that executes a function n times
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
def run_n_times(n): | |
def identity(func): | |
def wrapped(*args, **kwargs): | |
for i in range(n): | |
response = func(*args, **kwargs) | |
return response | |
return wrapped | |
return identity | |
@run_n_times(10) | |
def print_hi(): | |
print("hi") | |
print_hi() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment