Let us say I have a module `myfunctions`

with two functions:

```
def functionA():
return'Function A chosen.'
def functionB():
return 'Function B chosen.'
```

Then, I have a module `silly`

which does something with these functions:

```
def sillyprinter():
print chosen_function
print chosen_function()
```

Then, In my main script, I have:

```
import myfunctions
import silly
class FunctionChooser():
def __init__(function_choice="A"):
self.choose_function_given_choice(function_choice)
def do_something_many_times(self):
for i in range(1000):
silly.sillyprinter()
```

The idea of `FunctionChooser`

is that it is initialized with some user given information regarding which function (A or B) I want to use from `myfunctions`

. Then, after this choice is made, the user can call `do_something_many_times`

, which makes use of yet another module, which will use the chosen function from `myfunctions`

many times.

In order to be efficient, I'd like to avoid the following:

**1) Choosing which function to use again and again -- so I'd like to make the choice once (during initialization), and then have it "saved" somehow. This requirement disqualifies designs like:**

```
def sillyprinter(chosen_function):
print chosen_function
print chosen_function()
# =================================
class FunctionChooser():
def __init__(function_choice="A"):
self.function_choice = function_choice
def choose_function_given_choice(self):
if self.function_choice == "A":
return myfunctions.functionA
elif self.function_choice == "B":
return myfunctions.functionB
def do_something_many_times(self):
for i in range(1000):
silly.silly_printer(self.choose_function_given_choice())
```

**2) storing the function I'd like to choose as a class attribute, since self calls are expensive, and the use case is numerical. This requirements disqualifies designs like:**

```
def sillyprinter(chosen_function):
print chosen_function
print chosen_function()
# =================================
class FunctionChooser():
def __init__(function_choice="A"):
self.function_choice = function_choice
def choose_function_given_choice(self):
if self.function_choice == "A":
self.chosen_function = myfunctions.functionA
elif self.function_choice == "B":
self.chosen_function = myfunctions.functionB
def do_something_many_times(self):
for i in range(1000):
silly.silly_printer(self.chosen_function)
```

My current idea is to do the following:

```
def sillyprinter(chosen_function):
print chosen_function
print chosen_function()
# =================================
class FunctionChooser():
def __init__(function_choice="A"):
self.function_choice = function_choice
def choose_function_given_choice(self):
if self.function_choice == "A":
self.chosen_function = myfunctions.functionA
elif self.function_choice == "B":
self.chosen_function = myfunctions.functionB
def do_something_many_times(self):
chosen_function = self.chosen_function
for i in range(1000):
silly.silly_printer(chosen_function)
```

It is only very slightly different than the design I disqualified in **2)**, in that it makes a `self`

call once and then stores that function for use as a local variable.

Is this the best way to design such "function choice" in Python, given my requirements? (see below)

**EDIT:** I figure I should make requirements clearer, in order to make the question less general

- The "function choice string" does not have to be a string -- it could be any variable that encodes for some sort of function choice.
- I want to avoid
`self.`

access calls (but perhaps`.`

access calls in general?) due to issues with`numba`

optimizing code with access calls: Design heuristics for writing Python classes that interact with `scipy.integrate.odeint`? - The "function choice" variable is only given once, so we can consider it to effectively be a constant.