I have some imported functions from package import fizz, buzz and I want to map them to a variable func from a string arg.
class Whatever(object):
def __init__(self, func_name='fizz'):
try:
self.func = vars()[func_name]
except KeyError:
print "Bad func_name"
raise
But vars() only captures the local namespace, and I don't want to allow access to globals(). Any ideas? Or should I just go with if-else logic?
def __init__(self, func=fizz)globals, or even worse, something likeexecoreval.execandeval. Also thinking I could just create a class attr dict that maps the strings to the imported functions.