Last active
February 21, 2020 13:08
-
-
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
This file contains hidden or 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
| 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