Skip to content

Instantly share code, notes, and snippets.

@mchesterkadwell
Last active February 21, 2020 13:08
Show Gist options
  • Select an option

  • Save mchesterkadwell/563847e971eefe882ef82ccd3c039fb0 to your computer and use it in GitHub Desktop.

Select an option

Save mchesterkadwell/563847e971eefe882ef82ccd3c039fb0 to your computer and use it in GitHub Desktop.
Inspect a Python object for name, type, class, id, value, if callable, attributes and doc
def inspect(item, dir_=False):
print('--------------------------')
if hasattr(item, '__name__'):
print('Name: '), item.__name__
print(f'Type: {type(item)}'),
if hasattr(item, '__class__'):
print(f'Class: {item.__class__.__name__}')
print(f'Id: {id(item)}')
print(f'Value: {repr(item)}'),
print(f'Callable: {callable(item)}')
if dir_:
dir_attrs = [i for i in dir(item) if not i.startswith('_')]
print('Attributes (without underscores):')
print(*dir_attrs, sep=', ')
if hasattr(item, '__doc__'):
doc = getattr(item, '__doc__')
print(f'Doc: {doc}')
print('--------------------------')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment