Skip to content

Instantly share code, notes, and snippets.

@astpierre
Created August 17, 2019 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astpierre/1801eacb90ce684c04c1761b0538d612 to your computer and use it in GitHub Desktop.
Save astpierre/1801eacb90ce684c04c1761b0538d612 to your computer and use it in GitHub Desktop.
Example usage of decorators in Python
import os
import sys
from time import sleep
# Decorator @timer
def timer(func):
def f(*args, **kwargs):
before = time()
rv = func(*args, **kwargs)
after = time()
print(f'Time spent in {func.__name__}: {after-before}s')
return rv
return f
@timer
def main():
sleep(3)
return None
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment