I have a bunch of functions in Python out1, out2, out3 etc. and would like to call them based on an integer I pass in.
def arryofPointersToFns (value):
Actually, I have exactly this problem and it is quite realistic: I needed to display a table where each row requires a quite different method to compose the cell content. My solution was to create a class that returns an empty value, then subclass it and implement different value methods, then instantiate each subclass into an array, then call the instance's method depending on the row number. Global namespace polution is limited by making the subclasses inner to the table generator class. The code looks something like this:
class Table(object):
class Row(object):
def name(self):
return self.__class__.__name__
class Row1(Row):
def value(self):
return 'A'
class Row2(Row):
def value(self):
return 'B'
class Row3(Row):
def value(self):
return 'C'
def __init__(self):
self._rows = []
for row in self.Row.__subclasses__():
self._row.append(row())
def number_of_rows(self):
return len(self._rows)
def value(self,rownumber):
return self._rows[rownumber].value()
Obviously, in a realistic example, each of the subclass value methods would be quite different. The 'name' method is included to indicate how to provide a row title, if needed, using the arbitrary name of the inner class. This approach also has the advantage that one can easily implement a suitable 'size' method. The rows will appear in the output in the same order they appear in the code, but this may be an advantage.
Caution: the above is not tested code, just a precis of my actual code laid out to illustrate an approach.