Skip to content

Instantly share code, notes, and snippets.

@eas604
Created November 26, 2014 17:34
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 eas604/84b34cf47665cfa6ffcd to your computer and use it in GitHub Desktop.
Save eas604/84b34cf47665cfa6ffcd to your computer and use it in GitHub Desktop.
Modified version of the function from Dive Into Python.
def info(obj, spacing=10, collapse=True):
"""Print methods and doc strings.
:param obj: object to inspect
:type obj: module or class or list or dict or str or unicode
:param spacing: left column width
:type spacing: int
:param collapse: whether to collapse the text when printing
:type collapse: bool
"""
# Modified version of the function from Dive Into Python.
# http://www.diveintopython.net/power_of_introspection/index.html#apihelper.divein
# Governed by the Python License. https://docs.python.org/2/license.html
assert isinstance(spacing, int) and spacing > 0
method_list = [method for method in dir(obj) if callable(getattr(obj, method))]
process_func = collapse and (lambda s: ' '.join(s.split())) or (lambda s: s)
print('\n'.join(['{} {}'.format(
method.ljust(spacing), process_func(str(getattr(obj, method).__doc__))) for method in method_list]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment