I get the below error when I try to run this code. I'm trying to initialize a list with function pointers. It appears the eval doesn't see the functions. I'm guessing there's some kind of scoping going on that I don't understand. It works when I create the table by hand as you can see. I'm trying to avoid having to maintain a list of functions. This also worked when I made the functions global.
Traceback (most recent call last):
File "/home/westgate/code/python/try/x.py", line 27, in <module>
main()
File "/home/westgate/code/python/try/x.py", line 23, in main
fxns_eval = [eval(fxn_name) for fxn_name in dir() if fxn_name.startswith('fxn_')]
File "/home/westgate/code/python/try/x.py", line 23, in <listcomp>
fxns_eval = [eval(fxn_name) for fxn_name in dir() if fxn_name.startswith('fxn_')]
File "<string>", line 1, in <module>
NameError: name 'fxn_bar' is not defined
import inspect
def main():
def fxn_foo():
print('in foo '+inspect.currentframe().f_code.co_name)
def fxn_bar():
print('in bar '+inspect.currentframe().f_code.co_name)
for i in dir():
if i.startswith('fxn_'):
print(i)
fxn_bar()
fxns = [ fxn_foo, fxn_bar ]
fxns[1]()
fxns_eval = [eval(fxn_name) for fxn_name in dir() if fxn_name.startswith('fxn_')]
fxns_eval[1]()
main()