Skip to content

Instantly share code, notes, and snippets.

@mattkatz
Last active August 3, 2018 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattkatz/0c4f837949fbb5bdfd26171ff8552cf6 to your computer and use it in GitHub Desktop.
Save mattkatz/0c4f837949fbb5bdfd26171ff8552cf6 to your computer and use it in GitHub Desktop.
A dirty decorator for logging the docstrings of methods as they get called.
from functools import wraps
# sometimes you need to do a bunch of tracing and it is easier to just see docstrings flow by rather than write print statements.
def logdoc(func):
'''Wraps a function and logs the docstring of that function whenever it is called
Just decorate the function:
@logdoc
def foo(bar):
"""prints the bar and returns twice the bar"""
print(bar)
return 2*bar
>>> foo(4)
"prints the bar and returns twice the bar"
"4"
8
'''
@wraps(func)
def logdoc_wrapper(*args, **kwargs):
print(func.__doc__)
return func(*args, **kwargs)
return logdoc_wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment