This post originated from an RSS feed registered with Python Buzz
by maxim khesin.
Original Post: Inspector Python (in praise of shell)
Feed Title: python and the web
Feed URL: http://feeds.feedburner.com/PythonAndTheWeb
Feed Description: blog dedicated to python and the networks we live in
The importance of Python's interactive shell has been pointed out before, so I just want to add some drops to the sea.
The interactive shell feature was one of the first pointed out in Guido's talk at Google last week. People coming from static language background (mea culpa) should realize that working in the shell is not just some extra bolt-on, it is really a modus operandi for many serious Python (or other REPL language, but I will talk of Python) programmers.
In Python shell, you do not 'create' a program , not until *after* you developed it! You really 'live' in the program, work with live objects, introspect them and the docs, and take some notes for later so you can save your thougts. Unfortunately the integration between shell mode and program mode is not so great in the editors I've used, so you end up just pasting chunks of code around. (I have once seen an editor that claimed better integration, but I forgot what it was).
Beside working with objects a lot of what I do in a shell is looking up docs. I use help() a lot, sometimes dir() helps even more. A recent habit I aquired that seems to be paying off nicely is the inspect module. I highly recommend it, as it adresses the same needs as help() and dir() in an arguably superior way. Instead of giving you the docs for a class or function, it can actually give you the entire code. inspect.getsource() is the ticket.
For example, I needed to look up some parameters to django.db.models.Field. The old me would've started searching the docs. But with inspect I just do this: