Explore Anything in IPython
Sometimes in an IPython REPL, you just need to quickly unpack an object to see what attributes an object has. I tend to need to do this when I’m exploring a new library and want to see what kind of methods are available on the classes they define, or when I need to walk through a large nested dictionary.
Within my startup script, I define a handy little function that’s loaded into every IPython REPL to help out with this:
# ~/.ipython/profile_default/startup/00.start.py
def __explore(obj):
if isinstance(obj, dict):
for k, v in obj.items():
print(f"{k:16} ({type(v).__name__})")
elif isinstance(obj, list):
for k in obj:
print(f"({type(getattr(obj, k)).__name__})")
else:
for k in dir(obj):
print(f"{k:16} ({type(getattr(obj, k)).__name__})")Usage:
In [1]: __explore(my_object)What it does:
Quickly inspect attributes/keys and their types for:
- Dictionaries
- Lists (limited)
- General objects