Solved

How to find all the method available in a module in Python

Posted on 2016-11-16
1
76 Views
Last Modified: 2016-11-27
If I just use the python interpreter interactively and import a module then if I type module.<double tab> then it can show me all the module available

Thanks
0
Comment
Question by:beer9
1 Comment
 
LVL 1

Accepted Solution

by:
Fermi Paradox earned 500 total points
ID: 41897198
You can see all attributes of a module with dir().

Let's assume you wanted to check the contents of `re` module (obviously you can change that for whichever module you want). Then you'd do the following:

>>> import re
>>> dir(re)
['DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', '_alphanum', '_cache', '_cache_repl', '_compile', '_compile_repl', '_expand', '_locale', '_pattern_type', '_pickle', '_subx', 'compile', 'copy_reg', 'error', 'escape', 'findall', 'finditer', 'match', 'purge', 'search', 'split', 'sre_compile', 'sre_parse', 'sub', 'subn', 'sys', 'template']

Open in new window


In order to filter out anything that is not a method you can use the inspect module instead:

>>> import inspect
>>> [i[0] for i in inspect.getmembers(re, inspect.isfunction)]
['_compile', '_compile_repl', '_expand', '_pickle', '_subx', 'compile', 'escape', 'findall', 'finditer', 'match', 'purge', 'search', 'split', 'sub', 'subn', 'template']

Open in new window


The problem is that you'd get all non-public functions which (usually) you shouldn't be using or rely on. They start with _ so they are easy to filter out by:
>>> [i[0] for i in inspect.getmembers(re, inspect.isfunction) if not i[0].startswith('_')]
['compile', 'escape', 'findall', 'finditer', 'match', 'purge', 'search', 'split', 'sub', 'subn', 'template']

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question