Skip to content

Instantly share code, notes, and snippets.

@vsaraph
Last active April 5, 2021 21:46
Show Gist options
  • Save vsaraph/ad9aae917bc540749066f01cc341229b to your computer and use it in GitHub Desktop.
Save vsaraph/ad9aae917bc540749066f01cc341229b to your computer and use it in GitHub Desktop.
Python decorator that executes a function n times
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