Solved

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

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
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. …
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 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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now