Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I list the methods in a Python 2.5 module?

I'm trying to use a Python library written in C that has no documentation of any kind. I want to use introspection to at least see what methods and classes are in the modules. Does somebody have a function or library I can use to list the functions (with argument lists) and classes (with methods and member variables) within a module?

I found this article about Python introspection, but I'm pretty sure it doesn't apply to Python 2.5. Thanks for the help.

like image 825
Evan Kroske Avatar asked Aug 15 '09 00:08

Evan Kroske


People also ask

How do I list all methods in a Python module?

You can use dir(module) to see all available methods/attributes.

How do I see all methods in Python?

To list the methods for this class, one approach is to use the dir() function in Python. The dir() function will return all functions and properties of the class.

How do I display the contents of a Python module?

Python gives you several different ways to view package content. The method that most developers use is to work with the dir() function, which tells you about the attributes that the package provides. Function attributes are automatically generated by Python for you.

How do I explore a Python module?

Use the built-in dir() function to interactively explore Python modules and classes from an interpreter session. The help() built-in lets you browse through the documentation right from your interpreter.


2 Answers

Here are some things you can do at least:

import module  print dir(module) # Find functions of interest.  # For each function of interest: help(module.interesting_function) print module.interesting_function.func_defaults 
like image 157
Alexander Ljungberg Avatar answered Sep 21 '22 01:09

Alexander Ljungberg


Mark Pilgrim's chapter 4, which you mention, does actually apply just fine to Python 2.5 (and any other recent 2.* version, thanks to backwards compatibility). Mark doesn't mention help, but I see other answers do.

One key bit that nobody (including Mark;-) seems to have mentioned is inspect, an excellent module in Python's standard library that really helps with advanced introspection.

like image 26
Alex Martelli Avatar answered Sep 22 '22 01:09

Alex Martelli