Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding All Defined Functions in Python Environment

Is there a method to find all functions that were defined in a python environment?

For instance, if I had

def test:
   pass

some_command_here would return test

like image 818
Andy Avatar asked Jun 18 '13 18:06

Andy


People also ask

How do I see all the functions in a Python module?

We can list down all the functions present in a Python module by simply using the dir() method in the Python shell or in the command prompt shell.

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

The dir() function The dir() built-in function returns a sorted list of strings containing the names defined by a module. The list contains the names of all the modules, variables, and functions that are defined in a module.

How many user-defined functions in Python?

In this tutorial you will learn user defined functions in Python with the help of examples. In the last tutorial of Python functions, we discussed that there are two types of functions in Python: Built-in functions and user defined functions.


1 Answers

You can use inspect module:

import inspect
import sys


def test():
    pass

functions = [name for name, obj in inspect.getmembers(sys.modules[__name__], inspect.isfunction)]
print functions

prints:

['test']
like image 62
alecxe Avatar answered Sep 29 '22 03:09

alecxe