Created
August 17, 2019 17:41
-
-
Save astpierre/1801eacb90ce684c04c1761b0538d612 to your computer and use it in GitHub Desktop.
Example usage of decorators in Python
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 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