24

Ok I know you can use the dir() method to list everything in a module, but is there any way to see only the functions that are defined in that module? For example, assume my module looks like this:

from datetime import date, datetime

def test():
    return "This is a real method"

Even if i use inspect() to filter out the builtins, I'm still left with anything that was imported. E.g I'll see:

['date', 'datetime', 'test']

Is there any way to exclude imports? Or another way to find out what's defined in a module?

Cory
  • 22,772
  • 19
  • 94
  • 91
  • 1
    What's wrong with reading the source? – S.Lott Jul 09 '09 at 23:14
  • In Python, "method" is generally only used to refer to functions that are attributes of a class. Your `test()` is better referred to as a "function". – Miles Jul 10 '09 at 00:06
  • Does this answer your question? [Getting a list of locally-defined functions in python](https://stackoverflow.com/questions/18451541/getting-a-list-of-locally-defined-functions-in-python) – ggorlen Jul 12 '20 at 01:58

6 Answers6

31

Are you looking for something like this?

import sys, inspect

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__.itervalues() 
            if is_mod_function(mod, func)]


print 'functions in current module:\n', list_functions(sys.modules[__name__])
print 'functions in inspect module:\n', list_functions(inspect)

EDIT: Changed variable names from 'meth' to 'func' to avoid confusion (we're dealing with functions, not methods, here).

ars
  • 120,335
  • 23
  • 147
  • 134
  • @ars Couldn't you just replace your functions with `[f[1] for f in inspect.getmembers(mod) if inspect.isfunction(f[1])]` and get the same results? – mVChr Aug 20 '14 at 22:37
  • Nevermind, that would include imported functions, still need the `getmodule(func) == mod` for it to work, but still one-ish liner. – mVChr Aug 20 '14 at 22:43
  • Is there a way to make this work with decorated functions? – joshreesjones Jul 21 '15 at 16:27
5

How about the following:

grep ^def my_module.py
David Locke
  • 17,926
  • 9
  • 33
  • 53
  • It's a quick and dirty solution that works. It may not be the best solution for your problem but I wouldn't know because I don't know what problem you're trying to solve. – David Locke Jul 09 '09 at 23:11
2

You can check __module__ attribute of the function in question. I say "function" because a method belongs to a class usually ;-).

BTW, a class actually also has __module__ attribute.

1

Every class in python has a __module__ attribute. You can use its value to perform filtering. Take a look at example 6.14 in dive into python

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
0

the python inspect module is probably what you're looking for here.

import inspect
if inspect.ismethod(methodInQuestion):
    pass # It's a method
Ian P
  • 1,512
  • 3
  • 15
  • 18
  • Looks like it would work in combination with Stefano's answer. You can see what's a method and what's imported. – Ian P Jul 09 '09 at 23:02
  • 1
    I'm commenting on this post because my rep is under 50. Functions have __module__ attribute too. – Ian P Jul 09 '09 at 23:22
0
import inspect, sys

def get_module_func(module):
    is_function_in_module = lambda obj: inspect.isfunction(obj) and inspect.getmodule(obj) == module
    return inspect.getmembers(module, is_function_in_module)

get_module_func(sys.modules[__name__])
pgov
  • 21
  • 3
  • 2
    Answer needs supporting information Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](https://stackoverflow.com/help/how-to-answer). – moken Jul 30 '23 at 10:19