Created
July 13, 2013 18:24
-
-
Save hiroakis/5991680 to your computer and use it in GitHub Desktop.
Create my own decorator using functools.wraps.
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 functools | |
def ore(func): | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
print "@ore : oreoreoreoreore" | |
return func(*args, **kwargs) | |
return wrapper | |
def plus(x, y): | |
def decorator(func): | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
print "@plus(x, y) : %d" % (x + y) | |
return func(*args, **kwargs) | |
return wrapper | |
return decorator | |
def after(tm): | |
def decorator(func): | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
func(*args, **kwargs) | |
import time | |
print "@after(tm) : sleep %s seconds...." % tm | |
time.sleep(tm) | |
return wrapper | |
return decorator | |
@ore | |
@plus(2, 5) | |
@after(5) | |
def main(n): | |
""" main function """ | |
print n | |
if __name__ == '__main__': | |
main('mainmainmainmain') | |
print "__doc__ : %s" % main.__doc__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment