Skip to content

Instantly share code, notes, and snippets.

@javiromero
Created June 14, 2017 18:18
Show Gist options
  • Save javiromero/af60be1bf6b055eb7ffd4ad704adf810 to your computer and use it in GitHub Desktop.
Save javiromero/af60be1bf6b055eb7ffd4ad704adf810 to your computer and use it in GitHub Desktop.
Recursively explore an instance and print its component's types
def show_primitive_types(instance, depth=0):
"""
Recursively explore an instance and print its components' types.
"""
try:
for attr in vars(instance):
print('\t' * depth, attr, type(getattr(instance, attr)))
show_primitive_types(getattr(instance, attr), depth+1)
except TypeError:
pass
if isinstance(instance, list):
for each in instance:
show_primitive_types(each, depth+1)
if isinstance(instance, dict):
for k,v in instance.items():
show_primitive_types(v, depth+1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment