Exploring Python Objects with the dir and type functions

Two of my favorite functions for interactively exploring Python objects are dir and type. Calling the dir function with no attributes returns information about what’s defined in the current module — either by default when you start the python interpreter, or because you’ve done imports, defined variables and functions and classes, etc. Calling dir and passing in a named entity in Python returns the attributes of the object it is called on. In both cases, the results are returned as an ordered list, and python pprint can be used to “pretty print” the output.

The type function is much simpler, and returns the type to which the object belongs.

Experimenting with these two functions, I recently came up with an interesting Python puzzler that I posted as a poll on LinkedIn.

Python Object Quiz — Which of these is not an object in Python?

The Solution

Think you know the answer? Want to check your answer against mine? Check out our YouTube video where we explain how to get the result using “dir” and “type”.

I also added this as my solution for my Day 1 of 100 Days of Python. A few of us have started this challenge. Want to join us? Check out the Zen of 100 Days of Python.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.