How do I list all functions for a Python module ignoring functions the module imports?

Viewed 2116

I have a Python module 'something.py' and I'd like to get a list of all of the functions I've written in this file. I've followed this SO post explaining the standard ways of doing this.

The trouble is I have import functions in something.py, so when trying to list my functions I also list the imported functions I don't care about. How can I get a list of the functions that I wrote in something.py?

4 Answers
import <modulename>
dir(modulename)

2nd line of code will give the list of functions present in the module

Eg:

import math
dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

Updating functions from this answer--Find functions explicitly defined in a module from Python 2 to 3 (i.e. replace itervalues() with values():

def is_mod_function(mod, func):
    ' checks that func is a function defined in module mod '
    return inspect.isfunction(func) and inspect.getmodule(func) == mod


def list_functions(mod):
    ' list of functions defined in module mod '
    return [func.__name__ for func in mod.__dict__.values() 
            if is_mod_function(mod, func)]

Usage

print(list_functions(something))  # Output: ['a1', 'b1']

File main.py (Main module)

import inspect
import something  # project module

def is_mod_function(mod, func):
    return inspect.isfunction(func) and inspect.getmodule(func) == mod


def list_functions(mod):
    return [func.__name__ for func in mod.__dict__.values() 
            if is_mod_function(mod, func)]

def list_functions1(mod):
    return [func.__name__ for func in mod.__dict__.itervalues() 
            if is_mod_function(mod, func)]


# Get list of functions defined only in module something
print(list_functions(something))

File something.py (something module)

from textwrap import dedent
from math import sin, cos
import numpy as np

def a1():
  pass

def b1():
  pass

Output

['a1', 'b1']  # shows only the functions defined in something.py

The getmembers() function includes imported functions, so you need more. One idea is to check the name of the module of the function.

from inspect import getmembers, isfunction
import something
import sys

mod = something

funcs = [
    f
    for _, f in getmembers(mod, isfunction)
    if f.__module__ == mod.__name__
]

for f in funcs:
    print(f)

Or you could do the filtering with this conditional instead:

if sys.modules[f.__module__] is mod

Either way, the output is:

<function bar at 0x107fb05f0>
<function foo at 0x107f840e0>

My something module has this:

import sys
from textwrap import dedent

fubb = 1

def foo():
    pass

def bar():
    pass
Related