Solved

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

Posted on 2016-11-16
1
93 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Scrapy python: SSL handhshake failure  for GET request 1 550
Python algorithm 20 162
.properties file to call function/method 9 63
Python Regex Problem 24 148
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

730 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