Want to find out what methods are in a particular class or module or whatever?

Why did I bother to write a blog post about this? Apparently my blog has a ridiculous Google ranking so I’m just trying to help out.
Feel free to add more dirty debugging tips in comments. I’m also a fan of ‘print something.__class__’
17 Comments
Joey
Even better:
import pprint
pprint.pprint(dir(mod_or_obj))
Also good:
print ‘, ‘.join(dir(mod_or_obj))
mikeal
I can’t even express how happy I am with ipython.
obj.
Oh, i love it.
Alcides Fonseca
Before I knew about dir(), I actually used import inspect; inspect.getmembers(obj). When I decided to see the source to learn how it’s printing me the contents of the file, I saw it was using dir, but making it prettier.
So here’s an alternative.
kenkeiter
Long time no talk! I suppose you’ve been pretty busy though, so no worries. I was disappointed that I didn’t get to see you when you were up in Portland — I think I would have been able to mitigate your pizza problem haha (I’m pretty much a connoisseur). Anyway, I hope life is going well for you!
Ooh, and you may want to consider blurring out Virgil’s email addy from the message headers. I did it for you and posted it on my CDN — you can just link it over if you want: http://cdn.sixpixelsapart.com/images/leahculver_com__python-dir-chat.png
All the best!
-Ken
Ray Slakinski
I’m a big fan of help(module) as well, as long as the dev put in some decent comments that is
And please try TextMate Leah! you have no idea what your missing… make sure you add the django bundle
Dean Landolt
“I’m also a fan of ‘print something.__class__’”
Did you mean ‘print something.__doc__’? That’s a lot more helpful (when using a module who’s author was kind enough to use docstrings).
Bulkan Evcimen
Try;
import pdb; pdb.set_trace()
anywhere in the code and when that line gets executed you will drop into the Python Debugger.
Ben Walton
I’ve just always used…
obj.
in the console.
Ben Walton
That was supposed to be
obj.(tab)(tab)
wiz
import pdb; pdb.set_trace() — very povurful toy.
Tyler Menezes
I see why people like Python. In PHP to list directory contents you +just+ do:
$dir = dir(”module”);
while(($file = $dir->read()) !== false)
echo $file;
$dir->close();
Tom
Of course by posting this as an image it’s not indexable by Google…
Isaac
> I see why people like Python. In PHP to list
> directory contents you +just+ do:
> $dir = dir(”module”);
> while(($file = $dir->read()) !== false)
> echo $file;
> $dir->close();
dir() in python, according to the post, doesn’t do that at all. It lists the contents of an *object*, which means whoever named dir() in python failed.
The equivalent in php is as easy as in python.
print_r($object)
Anonymous
hi from 4chan!
Skytak
Meh, if you want it short in PHP, just do print_r(glob(”module/*”));
Sean F
vim omni-completion FTW
Mark Peloquin
I see nobody’s ever heard of pydoc. Much like perldoc, you can look up documentation on any module, class, function, method, etc from the CLI.
$ pydoc os
$ pydoc os.path
$ pydoc os.path.join