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
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']
Use globals() and types.FunctionType
>>> from types import FunctionType
>>> functions = [x for x in globals().values() if isinstance( x, FunctionType)]
Demo:
from types import FunctionType
def func():pass
print [x for x in globals().values() if isinstance(x, FunctionType)]
#[<function func at 0xb74d795c>]
#to return just name
print [x for x in globals().keys() if isinstance(globals()[x], FunctionType)]
#['func']
First, we will create the test function we want to find.
def test():
pass
Next, we will create the some_command_here function that you want.
def some_command_here():
return filter(callable, globals().values())
Finally, we call the new function and convert the filter into a tuple for viewing.
tuple(some_command_here())
Note: This searches the current global namespace and returns anything callable (not just functions).
Example:
>>> def test():
pass
>>> def some_command_here():
return filter(callable, globals().values())
>>> tuple(some_command_here())
(<function test at 0x02F78660>,
<class '_frozen_importlib.BuiltinImporter'>,
<function some_command_here at 0x02FAFDF8>)
>>>