Get all the functions of a Python module

You can get all the functions of a module using inspect module.

import statistics
from inspect import getmembers, isfunction

for f in getmembers(statistics):
    if isfunction(f[1]):
        print(f)

'''
('_coerce', <function _coerce at 0x1133ca280>)
('_convert', <function _convert at 0x1133e50d0>)
('_exact_ratio', <function _exact_ratio at 0x1133e5040>)
('_fail_neg', <function _fail_neg at 0x1133e5280>)
('_find_lteq', <function _find_lteq at 0x1133e5160>)
('_find_rteq', <function _find_rteq at 0x1133e51f0>)
('_isfinite', <function _isfinite at 0x1133ca160>)
('_ss', <function _ss at 0x1133e5940>)
('_sum', <function _sum at 0x11328a040>)
('fmean', <function fmean at 0x1133e53a0>)
('geometric_mean', <function geometric_mean at 0x1133e5430>)
('harmonic_mean', <function harmonic_mean at 0x1133e54c0>)
('mean', <function mean at 0x1133e5310>)
('median', <function median at 0x1133e5550>)
('median_grouped', <function median_grouped at 0x1133e5700>)
('median_high', <function median_high at 0x1133e5670>)
('median_low', <function median_low at 0x1133e55e0>)
('mode', <function mode at 0x1133e5790>)
('multimode', <function multimode at 0x1133e5820>)
('pstdev', <function pstdev at 0x1133e5b80>)
('pvariance', <function pvariance at 0x1133e5a60>)
('quantiles', <function quantiles at 0x1133e58b0>)
('stdev', <function stdev at 0x1133e5af0>)
('variance', <function variance at 0x1133e59d0>)
'''

inspect has getmembers function that returns all the functions and attributes. To get only functions, use isfunction method to check if each item is a function or not. Each is a tuple of name and type.

The following code shows how to get a list of all the functions of a module.

import statistics
from inspect import getmembers, isfunction

a = []

for f in getmembers(statistics):
    if isfunction(f[1]):
        a.append(f[0])

print(a)
# ['_coerce', '_convert', '_exact_ratio', '_fail_neg', '_find_lteq', '_find_rteq', '_isfinite', '_ss', '_sum', 'fmean', 'geometric_mean', 'harmonic_mean', 'mean', 'median', 'median_grouped', 'median_high', 'median_low', 'mode', 'multimode', 'pstdev', 'pvariance', 'quantiles', 'stdev', 'variance']

Python Tips

Python Tutorial