This is a sample code the recreates an error I am getting in my project. Maybe I am missing something, but cannot seem to understand what I am doing wrong here.
I have two files:
my_functions.py which contains the following code
func_dict = {'f1': func1, 'f2': func2}
def func1():
print('func1')
def func2():
print('func2')
and a main.py that tries to load function dictionary and run each function:
from my_functions import func_dict
func_dict['f1']()
func_dict['f2']()
When I run main.py, I get an error on from my_functions import func_dict
which says:
Exception has occurred: NameError name 'func1' is not defined
How do I fix this? If I am doing something wrong, then please do correct me. I am basically trying to load a dictionary that relates easier to remember keys to more complicated function names in the module.
Please place the 2 functions over the dictionary definition as the following and try again. That should fix the NameError -
def func1():
print('func1')
def func2():
print('func2')
func_dict = {'f1': func1, 'f2': func2}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.