I am writing a Python3 program, in which I need to be able to override some method on the fly. My folder structure is more like:
./
prog.py
methods/
add.py
minus.py
In prog.py
I want to call a calc()
function which is defined both in add.py
and minus.py
. I wish that the code can work as if:
def prog('foo'):
from method.foo import calc
calc()
But importing in the middle of a function seems awful and might slow down the whole program. Is there any workaround that can achieve the same effect?
I am trying to be flexible so that I can add more methods later on, so I avoid if statements and import all the modules at once.