Ever wanted to take a look at a snippet of Python code on the fly

Ever wanted to have a quick look at the source code of the module you are calling without having to browse to the file and open it, searching for the bit you need.
Lets take an easy example, say we are working with String object and want to know about swapcase
Try this in a shell

import inspect
inspect.getsource(string.swapcase)

This returns a little snippet of code (def swapcase) to the shell from the /usr/lib/python2.5/string.py file (Linux path)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s