Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all functions in a Python module?

I have a Python module installed on my system and I'd like to be able to see what functions/classes/methods are available in it.

I want to call the help function on each one. In Ruby I can do something like ClassName.methods to get a list of all the methods available on that class. Is there something similar in Python?

eg. something like:

from somemodule import foo print(foo.methods)  # or whatever is the correct method to call 
like image 348
Chris Gow Avatar asked Sep 26 '08 12:09

Chris Gow


People also ask

How do you list all functions in a Python class?

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.

Which library function returns the list of all functions in a module?

dir() is a built-in function that also returns the list of all attributes and functions in a module.

Which function is used to list built in functions in a module?

Python Modules List. Using Python built-in function help(), we can obtain the list of built-in modules available in Python.


2 Answers

You can use dir(module) to see all available methods/attributes. Also check out PyDocs.

like image 183
camflan Avatar answered Oct 01 '22 00:10

camflan


Use the inspect module:

from inspect import getmembers, isfunction  from somemodule import foo print(getmembers(foo, isfunction)) 

Also see the pydoc module, the help() function in the interactive interpreter and the pydoc command-line tool which generates the documentation you are after. You can just give them the class you wish to see the documentation of. They can also generate, for instance, HTML output and write it to disk.

like image 23
Thomas Wouters Avatar answered Sep 30 '22 23:09

Thomas Wouters