Array of Functions
richard_riehle
rriehle at itu.edu
Tue May 26 20:43:31 EDT 2015
I realized that I mentioned earlier that I found a solution to my original question, but that I never posted an example of the solution. So, here is a simplified example for anyone who is interested.
def fArray(fselect, fparm = 1):
def A1(p = fparm):
if p == 1:
print("printing A1[1]")
else:
print("printing A1[other]")
def A2(p = fparm):
if p == 1:
print("printing A2[1]")
else:
print("printing A2[other]")
A = [A1, A2]
A[fselect]()
In this example, I enclosed two functions within another function, and then put those two functions in a list. Then, with appropriate parameters, I called one of the functions in the list, associated the formal parameter with the function in a call to the array, and presto, it performs the function.
The more advanced problem I wanted to solve, a two dimensional array of functions, once this example is understood, becomes trivial to implement. In fact, the more interesting problem I wanted to solve involved a dictionary of functions in a two-dimensional array, and that too was easy to do in Python.
When I compare what this would require in C, C++, Java, or most other languages, I find Python to be really easy for solving this kind of problem.
Next, I plan to develop the solution using decorators and assertions to empower it with a greater level of portability and to make the functions more generic.
I hope someone finds this interesting.
Richard Riehle, PhD
More information about the Python-list
mailing list