Inspecting Python Objects

In the python REPL you sometimes need to check the properties and methods on an object. The dir() method is often used for this purpose.

>>> t = (1, 2, 3)
>>> dir(t)
['__add__', '__class__', '__contains__', '__delattr__', 
 '__dir__', '__doc__', '__eq__', '__format__', '__ge__', 
 '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', 
 '__hash__', '__init__', '__iter__', '__le__', '__len__', 
 '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', 
 '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', 
 '__sizeof__', '__str__', '__subclasshook__', 'count', 

A more readable alternative is the see module, which will format the output in a human readable way.

>>> from see import see
>>> see(t)
    []          in          +           *           <
    <=          ==          !=          >           >=
    dir()       hash()      help()      iter()      len()
    repr()      str()       .count()    .index()

You can install see with pip install see.

Posted on 16 November 2015 in python