You might consider making a dictionary to hold your methods. With a defaultdict, you can ensure that do_exit
is called if it's ever the case that nothing else was slotted in for a particular function name. On the other hand, this might not be very safe or validated against, e.g. spelling errors:
from collections import defaultdict
method_dict = defaultdict(lambda: do_exit)
# Try this
method_dict["do_quit"]()
Within a class, you could also override __getattr__
if you'd like. Say, just guessing, that all of these kinds of methods begin with do
or else maybe the condition is that they end with some synonym of complete
. You could give the class a class attribute that holds the appropriate convention items and checks for them, and looks them up in method_dict
as needed.
from collections import defaultdict
class Foo(object):
QUIT_WORDS = ['exit', 'quit', 'stop', 'finish', 'complete', 'leave']
def __init__(self):
self.method_dict = defaultdict(lambda: self.do_exit)
def __getattr__(self, attr):
if any([attr.endswith("_{}".format(x)) for x in self.QUIT_WORDS]):
return self.method_dict[attr]
else:
return super(Foo, self).__getattribute__(attr)
def do_exit(self):
print "Exit!"
For example:
In [88]: f = Foo()
In [89]: f.do_quit()
Exit!
In [90]: f.do_exit()
Exit!
In [91]: f.do_go_bye_bye()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-91-2584940dee36> in <module>()
----> 1 f.do_go_bye_bye()
<ipython-input-87-3b0db0bf6a47> in __getattr__(self, attr)
11 return self.method_dict[attr]
12 else:
---> 13 return super(Foo, self).__getattribute__(attr)
14
15