Related Entries

India PyCon 2009
Quick wallpaper changer
Load testing with Grinder
Adding namespace to XML
Opera RSS to OPML

« Joy of Python: Classes and Dictionaries
» SQLite and Python

Joy of python: dir, help and pprint

Three functions that can help you lookup Python's batteries - included ones and others too.

Python is a great language that was designed with documentation in mind from the ground up. Which means, you can always get documentation from Python's interpreter.

  1. dir - function to return a list of methods and attributes in an object
  2. help - get the documentation by introspection
  3. pprint - great module to pretty print lists and other things so that it is easily readable.

Here’s how it proves helpful.

PythonWin 2.2.1 (#34, Apr 15 2002, 09:51:39)...
>>> import string
>>> dir(string)
['_StringType', '__builtins__', '__doc__',...]
>>> help(string)
Help on module string:

    string - A collection of string operations (most are no longer used in Python 1.6).


    Warning: most of the code you see here isn’t normally used nowadays.  With
    Python 1.6, many of these functions are implemented as methods on the
    standard string object. 


>>> from pprint import pprint
>>> pprint(dir(string))

Very easy, is it not?

  1. Easy to forget those functions exist. And yes, easy. Thanks lots.

    Posted by: Tri Nguyen on February 20, 2003 11:56 AM