Created
December 5, 2018 13:54
-
-
Save cedeber/76f12015fa4175d4c4cd474546d9c95e to your computer and use it in GitHub Desktop.
Python decorator
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 time | |
from functools import wraps | |
# Use this function only if you need decorator parameters | |
def howmuchtime(limit=None): | |
def decorator(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
start = time.time() | |
res = func(*args, **kwargs) | |
total = time.time() - start | |
if limit is not None and total >= limit: | |
print(total) | |
return res | |
return wrapper | |
return decorator | |
@howmuchtime(0) | |
def hello(): | |
print("hello world") | |
hello() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment