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
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

  • By
    6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

Incredible Demos

  • By
    Add Styles to Console Statements

    I was recently checking out Google Plus because they implement some awesome effects.  I opened the console and same the following message: WARNING! Using this console may allow attackers to impersonate you and steal your information using an attack called Self-XSS. Do not enter or paste code that you...

  • By
    Animated 3D Flipping Menu with CSS

    CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more.  I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...

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!