Python, the learn-at-home language
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!
The Python, the learn-at-home language article by Aral Balkan, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial 2.0 UK: England License.

Subscribe to my blog






Mike Thompson
You may be interested in this alternative one liner:
[item for item in dir(os) if "dir" in item]
Keep up the interesting work you’re doing with GAE
August 6th, 2008 at 11:58 amAral
Oooh, of course, I still forget that strings are just lists of numbers (and, hey, I love my regular expressions)
Muchas dankes!
August 6th, 2008 at 6:11 pmFernando Perez
You may also try this one-liner in IPython:
os.*dir*?
A bit shorter
Cheers,
f
August 7th, 2008 at 9:26 amAral
Hey Fernando,
Ooooh, I liiike!
Doesn’t appear work in all situations (e.g., combined with lookup, etc.) but a very handy shortcut when peeking through the object hierarchy.
Thanks very much for sharing.
August 7th, 2008 at 6:34 pmUldis
@Fernando: wow, “?” rocks. it’s one of IPython goodies, isn’t it?
@Aral: you mentioned on Twitter that you like “inspect” (which I have not used yet). how / what are you using it for?
August 10th, 2008 at 10:02 pmFernando Perez
@Uldis: yes, ‘?’ is an ipython goodie, amongst a bunch of others
Try foo?? (two marks) for extra info.
August 11th, 2008 at 7:49 am