Python dir() is my fav debugging thing ever

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. :D Feel free to add more dirty debugging tips in comments. I’m also a fan of  ‘print something.__class__’

17 Comments

  1. Joey

    Posted August 10, 2008 at 11:44 am | Permalink

    Even better:

    import pprint
    pprint.pprint(dir(mod_or_obj))

    Also good:

    print ‘, ‘.join(dir(mod_or_obj))

  2. mikeal

    Posted August 10, 2008 at 11:46 am | Permalink

    I can’t even express how happy I am with ipython.

    obj.

    Oh, i love it.

  3. Posted August 10, 2008 at 11:52 am | Permalink

    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.

  4. Posted August 10, 2008 at 1:17 pm | Permalink

    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

  5. Posted August 10, 2008 at 4:19 pm | Permalink

    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

  6. Posted August 10, 2008 at 5:53 pm | Permalink

    “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).

  7. Posted August 10, 2008 at 8:14 pm | Permalink

    Try;

    import pdb; pdb.set_trace()

    anywhere in the code and when that line gets executed you will drop into the Python Debugger.

  8. Posted August 10, 2008 at 9:42 pm | Permalink

    I’ve just always used…
    obj.
    in the console.

  9. Posted August 10, 2008 at 9:43 pm | Permalink

    That was supposed to be
    obj.(tab)(tab)

  10. Posted August 10, 2008 at 11:32 pm | Permalink

    import pdb; pdb.set_trace() — very povurful toy.

  11. Posted August 11, 2008 at 4:27 pm | Permalink

    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();

  12. Tom

    Posted August 12, 2008 at 2:58 pm | Permalink

    Of course by posting this as an image it’s not indexable by Google…

  13. Posted August 15, 2008 at 1:49 pm | Permalink

    > 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)

  14. Anonymous

    Posted August 18, 2008 at 5:13 am | Permalink

    hi from 4chan!

  15. Skytak

    Posted August 18, 2008 at 10:31 pm | Permalink

    Meh, if you want it short in PHP, just do print_r(glob(”module/*”));

  16. Posted August 19, 2008 at 3:08 pm | Permalink

    vim omni-completion FTW :)

  17. Mark Peloquin

    Posted August 20, 2008 at 10:25 pm | Permalink

    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