How to list all functions in a Python module?

788    Asked by RutujaMishra in Python , Asked on Apr 7, 2021

I have a python module installed on my system and  want to see all functions/classes/methods available in it.

I want to call the doc function on each one. In ruby, I can do something like ClassName.methods to get a list of all the methods available in that class. Is there something similar in python? How to get python list functions in module.

 

eg. something like:

from somemodule import foo

print foo.methods # or whatever is the correct method to call

Answered by Rutuja Mishra

To list all functions in a Python module you can use dir(module).

You can also use the inspect module, it will also list all the functions in a Python module.

Use the below code for the same:-
from inspect import getmembers, isfunction
from my_project import my_module
functions_list = [o for o in getmembers(my_module)if isfunction(o[1])]

Your Answer

Interviews

Parent Categories