I currently have two Python 3 scripts: chooseFunc.py and testFuncs.py.
My goal is to write a function chooseFunction
in the former script, then import the function for usage within the latter script. Upon execution of chooseFunction()
in testFuncs.py, it should display the functions present in testFuncs.py and allow the user to choose which func()
to run. Thefunc
s have no args
for the purpose at hand.
I've created a function chooseFunction
as such:
def chooseFunction(module):
funcList = []
for key, value in module.__dict__.items():
if callable(value) and value.__name__ !='<lambda>' and value.__name__ !='chooseFunction':
funcList.append(value)
for funcIdx, myFunc in enumerate(funcList):
print(funcIdx, myFunc.__name__ )
chosenIdx = int(input('Choose function... ·\n'))
return funcList[chosenIdx]()
Currently, when executed in testFuncs.py as follows:
from chooseFunc import chooseFunction
import sys
def func1():
print('func1')
def func2():
print('func2')
chooseFunction(module=sys.modules[__name__])
the argument module=sys.modules[__name__]
on the last row has to be provided.
How do I change this behavior such that chooseFunction
retrieves the list of functions present in testFuncs.py, without having to provide the module
argument above?
Apologies in advance for any potential inconsistencies, this is my first entry on Stack Exchange... General feedback is appreciated!