2

How can I return the arguments of a function in a different module

#Module: functionss.py
def simple(a, b, c):
    print "does something"


#Module: extract.py
#load the called module and function
def get_args(module_name, function_name):
    modFile, modPath, modDesc = imp.find_module(module_name)
    mod = imp.load_module(module_name,modFile,modPath,modDesc)
    attr = getattr(mod, function_name)

#this is the part I don't get - how do I read the arguments
    return = attr.get_the_args()

 if __name__ == "__main__":
     print get_args("functions.py", "simple")

 #this would ideally print [a, b, c]
4
  • What's wrong with reading the source? It's Python -- you have the source, just read it. What's preventing that? Commented Nov 19, 2009 at 11:16
  • Further, what's wrong with the help() function? That usually tells you everything. What's wrong with that? Commented Nov 19, 2009 at 11:17
  • See stackoverflow.com/questions/847936/… Commented Nov 19, 2009 at 11:18
  • And stackoverflow.com/questions/741950/… Commented Nov 19, 2009 at 14:17

1 Answer 1

1

Use inspect.getargspec for the "heavy lifting" (introspecting a function).

Use __import__ to import a module (given its module name -- "functions.py" is a terrible way to specify a module name;-).

Use getattr(moduleobject, functionname) to get the function object given module object and function name.

Sign up to request clarification or add additional context in comments.

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.