Python, the learn-at-home language

One of the things that I love about Python is that it has all the documentation you ever need (all right, almost) in the code itself. Many moons ago, the very first framework I wrote in ActionScript (Flash 5) used the same technique by placing documentation on the activation objects of functions (and it would be cool to see that practice make a comeback in AS3.)

In Python, to find out what properties an object has, you just ask for a listing. The following, for example, shows you all properties and methods on the os module.

import os
dir(os)

In fact, I was doing this just today as I wanted to find out which functions are available for working with folders for the automatic restore feature I'm building for my Google App Engine backup solution.

Of course, that brings back a lot of items.

But have no fear, because we have regular expressions. I've grown to loooooove regular expressions thanks to being finally forced to learn them and use them daily in Django. I'm probably don't write the most concise ones but I've gotten to the point where I use them all the time and they simplify my life to no end.

So, to see just the methods that have "dir" in them:

import re
r = r'(^.*?dir.*?$)'
rc = re.compile(r)
matches = map(rc.match, dir(os))
[x.groups() for x in matches if not x == None]

And Bob's your uncle.

(Oh yeah, and list comprehensions rock too!)

And the coolest thing is that since Python is interpreted, I'm doing all this in the excellent IPython shell and using the language to learn the language and as a reference.

The more I use Python, the more I love it. They really did everything right. And that success, no doubt, is firmly planted in the unerring focus and the core values instilled in the language by Guido -- you're a freakin' genius, dude, and it gives me no end of confidence in Google App Engine that you're on the project! :)

Comments