Skip to content

Instantly share code, notes, and snippets.

@affo
Created October 31, 2014 16:52
Show Gist options
  • Save affo/17f2b13163ed5455edd3 to your computer and use it in GitHub Desktop.
Save affo/17f2b13163ed5455edd3 to your computer and use it in GitHub Desktop.
Annotations (decorators) with @wraps or not in python
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