0

Let's say I have a file called example.py with the following content:

def func_one(a):
    return 1

def func_two(b):
    return 2

def func_three(b):
    return 3

How would I get a list of function instances for that file? I have tried playing with the compiler,parser and ast modules but I can't really figure it out. It might be a lot easier than that, but it's slightly out of my depth.

EDIT: To be clear I don't want to import the functions.

5
  • Could you explain what "func instances" means here? Commented Mar 12, 2012 at 7:24
  • Is it OK to import the module? Commented Mar 12, 2012 at 7:27
  • Tkae a look: stackoverflow.com/questions/139180/… Commented Mar 12, 2012 at 7:27
  • nah i dont want to import it. i just want access to the function itself. Commented Mar 12, 2012 at 7:29
  • Without actually importing the module, the problem is much harder (and you'll miss things like import-time actions that populate the module's namespace without using the def keyword, unless you implement your own Python language...). Commented Mar 12, 2012 at 7:37

4 Answers 4

4

You can use inspect module:

>>> import inspect
>>> import example
>>> inspect.getmembers(example, inspect.isroutine)
[('func_one', <function func_one at 0x0238CBB0>), ('func_three', <functi
three at 0x0240ADB0>), ('func_two', <function func_two at 0x0240ADF0>)]
Sign up to request clarification or add additional context in comments.

8 Comments

yeah i don't want to import it though.
that's close i think. i want to do that without actually importing it
How do you wanna do it then? I mean at least the file will be imported if not the functions. What is your use case?
well i guess i could import them, but i don't want the functions to be callable from the interpreter and pollute the local namespace.
local namespace will only have the name of the module you imported. Not the functions which reside in it. The functions need to be qualified with the module name still.
|
0

You can use dir. This return the list of names in the current local scope. Although this might return more than function names.

>>> import utils
>>> utils.__name__
'utils'
>>> dir(utils)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'human_number', 'islice', 'math', 'prime_factors', 'primes_sieve', 'primes_sieve1', 'proper_divisors', 'pyth_triplet', 'square_of_sum', 'sum_of_nums', 'sum_of_squares', 'test', 'triangle_nums', 'window']

Comments

0

You could use globals() function, it is return a dict with all global environment where you can find you functions. Example:

d = globals().copy()
l = []
[l.append(k) for k in d if k.startswith('func')]

Comments

0

If you really don't want to import into the namespace like this:

import examples
print dir(examples)

['__builtins__', '__doc__', '__file__', '__name__', 
'__package__', 'func_one', 'func_three', 'func_two'] #OUTPUT

You could open and read the file, doing a search for any defs and append those to a list, thus returning all function, and method definition names.

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.