I have two files;
The first file, myProgram.py, is a python file with a number of functions
one of the functions contained inside is myFunction
this function would be called with
import myProgram
myProgram.thisFunction()
myProgram.thatFunction()
The second file contains a menu system, with calls to the functions in myProgram.py
I'd like to call a particular function based on an argument passed to a function in file2
def file2_function(function):
myProgram.function
file2_function(thisFunction(x,y,z))
which would essentially create myProgram.thisfunction(x,y,z) and execute it.
I guess I could do this using a bunch of if/elif statements, say:
def file2_function(function):
if function == 1:
myProgram.thisFunction
elif function == 2:
myProgram.thatFunction
Though that could get messy as I'm already using a lot of if/elif/else statements for the menu system.
Is there a better way (or is the if/elif route the way to go?)
解决方案
Alternatively you have getattr. See following snippet:
>>> import sys
>>> getattr(sys,'exc_info')
>>> getattr(sys,'exc_info')()
(None, None, None)