Get Class Methods with Python

By  on  

As a newbie to the excellent world of Python development, I'm not always familiar with the methods provided by imported classes.  Oftentimes these classes are well-documented but in the case that methods aren't documented, I found the dir function useful for getting a list of methods:

# dir({object})
dir(difflib)

"""
Returns:

['Differ', 'HtmlDiff', 'IS_CHARACTER_JUNK', 'IS_LINE_JUNK', 'Match', 'SequenceMatcher', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_calculate_ratio', '_count_leading', '_file_template', '_legend', '_mdiff', '_namedtuple', '_styles', '_table_template', '_test', 'context_diff', 'get_close_matches', 'heapq', 'ndiff', 'reduce', 'restore', 'unified_diff']

"""

The snippet above does exactly what you would expect -- provides a list of method names for the viewing!

Recent Features

  • By
    CSS Gradients

    With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements.  CSS gradients are another step in that direction.  Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome...

  • By
    I’m an Impostor

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

Incredible Demos

  • By
    Pure CSS Slide Up and Slide Down

    If I can avoid using JavaScript for element animations, I'm incredibly happy and driven to do so.  They're more efficient, don't require a JavaScript framework to manage steps, and they're more elegant.  One effect that is difficult to nail down with pure CSS is sliding up...

  • By
    MooTools Flashlight Effect

    Another reason that I love Twitter so much is that I'm able to check out what fellow developers think is interesting. Chris Coyier posted about a flashlight effect he found built with jQuery. While I agree with Chris that it's a little corny, it...

Discussion

  1. hwiechers

    Isn’t this normall done with dir(difflib)?

    • That does the same thing but alphabetizes the result. Thanks for mentioning that!

  2. Jasper

    It does not do the same thing. dir(my_object) will show you everything you can do on the instance, including methods on the class, and superclasses. __dict__ will only show you the unique things that belong to the object, which isn’t very many.

    • Thanks for the additional information Jasper! I’ve updated my post!

  3. Marcin

    You can also use bpython shell which is a nice alternative to ipython and standard python ones. It has auto-completion which displays all available methods and attributes on any object. It can be installed in virtualenv with pip install bpython.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!