2

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?

9
  • Are you sure the answers in your reference don't work? For instance in the comments of the answer by adnan we see that from inspect import getmembers, isfunction; import something; functions_list = getmembers(something, isfunction) provides a list only of functions in something module (i.e. functions of imports not included). Commented Aug 17, 2020 at 9:26
  • Unfortunately not, and I was surprised by this too. It works as expected when I comment my imports out but otherwise it fails. I suspect the problem is that I'm importing everything in my project's package which is probably causing it to behave differently than if I was just trying to import something from the standard library. Commented Aug 17, 2020 at 9:33
  • @Visipi--that's surprising since it works in my simple test program which 1) imports a module something and 2) something.py module imports something_else plus the math library. Commented Aug 17, 2020 at 9:42
  • Ah, thank you for sanity testing. Above and beyond. I'm quite sure now the problem is me. Probably something weird in my init file. I'll consider that answered but I'm not sure how to give you points for a comment. Commented Aug 17, 2020 at 9:50
  • @Visipi--actually in this case I think stackoverflow (SO) considers it an unsuitable question since the problem can't be reproduced (i.e. thus any answer can't be used by others). In which case, I think SO suggest removing the question, but I could be wrong. Commented Aug 17, 2020 at 9:55

4 Answers 4

6

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
Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

1
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']

1 Comment

Yes, but dir() also lists every imported function in that file For example, suppose I have numpy imported into something.py. When I print dir(something), I'm not interested in seeing things from numpy.
-1

You can use dir(yourModule) to return a list of attributes: https://docs.python.org/3/library/functions.html#dir

2 Comments

But dir(yourModule) also lists the functions I imported in 'yourModule.' I'm only interested in listing the functions I wrote.
yes you're right, sorry about that. I'm not sure if thats possible.. Have you figured it out already ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.