Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to list all functions in a module? [duplicate]

I defined a .py file in this format:

foo.py

def foo1(): pass
def foo2(): pass
def foo3(): pass

I import it from another file:

main.py

from foo import * 
# or
import foo

Is it possible list all functions name, e.g. ["foo1", "foo2", "foo3"]?


Thanks for your help, I made a class for what I want, pls comment if you have suggestion

class GetFuncViaStr(object):
    def __init__(self):
        d = {}
        import foo
        for y in [getattr(foo, x) for x in dir(foo)]:
            if callable(y):
               d[y.__name__] = y
    def __getattr__(self, val) :
        if not val in self.d :
           raise NotImplementedError
        else:
           return d[val] 
like image 519
user478514 Avatar asked Oct 28 '10 07:10

user478514


People also ask

How do you list all functions in a module?

Method 1: Using the dir() Function: We have first to import the module in the Python shell, and then we have to write the module name in the dir() method, and it will return the list of all functions present in a particular Python module.

How do I show all functions in a Python module?

To list all functions in a Python module you can use dir(module).

Which library function returns the list of all functions in a module?

dir() is a built-in function that also returns the list of all attributes and functions in a module.


3 Answers

The cleanest way to do these things is to use the inspect module. It has a getmembers function that takes a predicate as the second argument. You can use isfunction as the predicate.

 import inspect   all_functions = inspect.getmembers(module, inspect.isfunction) 

Now, all_functions will be a list of tuples where the first element is the name of the function and the second element is the function itself.

like image 177
aaronasterling Avatar answered Sep 23 '22 03:09

aaronasterling


you can use dir to explore a namespace.

import foo print dir(foo) 

Example: loading your foo in shell

>>> import foo >>> dir(foo) ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'foo1', 'foo2', 'foo3'] >>>  >>> getattr(foo, 'foo1') <function foo1 at 0x100430410> >>> k = getattr(foo, 'foo1') >>> k.__name__ 'foo1' >>> callable(k) True >>>  

You can use getattr to get the associated attribute in foo and find out if it callable.

Check the documentation : http://docs.python.org/tutorial/modules.html#the-dir-function

and if you do - "from foo import *" then the names are included in the namespace where you call this.

>>> from foo import * >>> dir() ['__builtins__', '__doc__', '__name__', '__package__', 'atexit', 'foo1', 'foo2', 'foo3'] >>>  

The following brief on introspection in python might help you :

  • http://www.ibm.com/developerworks/library/l-pyint.html
like image 25
pyfunc Avatar answered Sep 21 '22 03:09

pyfunc


Like aaronasterling said, you can use the getmembers functions from the inspect module to do this.

import inspect

name_func_tuples = inspect.getmembers(module, inspect.isfunction)
functions = dict(name_func_tuples)

However, this will include functions that have been defined elsewhere, but imported into that module's namespace.

If you want to get only the functions that have been defined in that module, use this snippet:

name_func_tuples = inspect.getmembers(module, inspect.isfunction)
name_func_tuples = [t for t in name_func_tuples if inspect.getmodule(t[1]) == module]
functions = dict(name_func_tuples)
like image 41
Flimm Avatar answered Sep 23 '22 03:09

Flimm