0

How can we find all the functions in a python program??? for eg.

Input

def func1:
  #doing something

def func2:
  #doing something

def func3:
  #doing something

Output

{'func1' , 'func2' , 'func3'}

2 Answers 2

1

If you want all functions in the global scope, you can use globals() with inspect.isfunction():

>>> def foo():
...     pass
... 
>>> def bar():
...     pass
... 
>>> import inspect
>>> [member.__name__ for member in globals().values() \
...                  if inspect.isfunction(member)]
['bar', 'foo']
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any diff. when I use 'locals()' instead of 'gloabals()' in line :: "for member in globals().values()" ???
There won't be any difference if you run this code from the global scope, but if you run it from, say, a function, the difference should become obvious.
1

Guessing you want only the methods in your current context:

import inspect

d = locals()
funcs = [f for f in d if inspect.isfunction(d[f])] 

Comments

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.