ProbableOdyssey | Blake Cook

Explore Anything in IPython

· 1 min read · 127 words

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:

#python #introspection #cli #object-attributes #list #object-inspection #dictionary #startup-scripts #ipython #programming #utility-functions #python-tips #software-engineering #debugging #repl

Reply to this post by email blZake@proZbableodyssey.blog (remove Z characters) ↪

Comments

Leave a comment

Markdown is supported. Your email is private and only used if you'd like a reply.