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):
#call outn where n = value
Is there an easy way to do this?
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):
#call outn where n = value
Is there an easy way to do this?
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:
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.
tl;dr: Write an
out(n)
function rather thanout1(), out2(), ..., outN()
and don't bother with this hack.I cannot imagine a reasonable scenario where this question would come up in practice. Please reconsider the architecture of the problem; there is likely to be a much better way to do this (because storing them in a list implies there is nothing meaningful about the functions except the index; for example, I can only imagine that you'd want to do this if you were creating a bunch of dynamically-generated thunks where their temporal ordering matters, or something similar). Especially any novice users you are reading this answer, consider making a more general function that can handle everything, or associating to each function some more identifying information, or sticking it as part of a class, etc.
That said, this is how you'd do it.
or
this is just the following two steps in one step:
or if you don't have a registry of functions 'myFunc' like the OP said above, you can use globals(), though it is extremely hackish form and to be avoided (unless you want those functions to be available in your module namespace, in which case maybe it's fine... but this is probably rarely the case, and you'd probably rather define those functions in a submodule then
from mysubmodule import *
them, which is in turn slightly frowned upon):here are two other ideas (added after answer was accepted and first two comments):
You can also create a decorator like this:
and use it like so:
then you can call reg.all['f1']. You could adapt the
reg
decorator to keep track of the indexing and do something like:Alternatively, to avoid
globals()
, you could define a class:If your number of functions is small, you could get away with
['f1','f2','f3'][i]
.Of course without further information, all these suggestions are just ignoring the real problem: this situation should never come up, and is a possibly a sign of a serious architecture flaw, when you'd probably rather have something (to use your example) like:
rather than