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
    Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

Incredible Demos

  • By
    Google Font API

    Google recently debuted a new web service called the Font API.  Google's Font API provides developers a means by which they may quickly and painlessly add custom fonts to their website.  Let's take a quick look at the ways by which the Google Font...

  • By
    Ana Tudor’s Favorite CodePen Demos

    Cocoon I love canvas, I love interactive demos and I don't think I have ever been more impressed by somebody's work than when I discovered what Tiffany Rayside has created on CodePen. So I had to start off with one of her interactive canvas pens, even though...

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!