Created
October 31, 2014 16:52
-
-
Save affo/17f2b13163ed5455edd3 to your computer and use it in GitHub Desktop.
Annotations (decorators) with @wraps or not 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
from functools import wraps | |
def logger_without_wrap(fn): | |
def _wrapper(*args, **kwargs): | |
print '# begin: ' + fn.__name__ | |
fn(*args, **kwargs) | |
print '# end: ' + fn.__name__ | |
return _wrapper | |
def logger_with_wrap(fn): | |
@wraps(fn) | |
def _wrapper(*args, **kwargs): | |
print '# begin: ' + fn.__name__ | |
fn(*args, **kwargs) | |
print '# end: ' + fn.__name__ | |
return _wrapper | |
@logger_without_wrap | |
def foo(): | |
''' | |
I am foo doc! | |
''' | |
print 'I am the body of foo!' | |
@logger_with_wrap | |
def bar(): | |
''' | |
I am bar doc! | |
''' | |
print 'I am the body of bar!' | |
print foo.__name__ | |
print foo.__doc__ | |
foo() | |
print bar.__name__ | |
print bar.__doc__ | |
bar() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment