我正在用 Python 创建一系列文本菜单。Python 约定告诉我,我应该只为需要维护和操作的数据成员创建类/对象。会这样我不确定.....下面会合适还是会创建一个基本功能,然后根据哪个菜单更好地装饰它?
class Menu(object):
def __init__(self):
# Ideally, self._options would be an empty dict for the base class.
# This is just for the sake of example.
self._options = {'a': self.optionA,
'b': self.optionB}
def handle_options(self, option):
if option not in self._options:
print "Invalid option"
# re-draw
return
self._options[option]()
def optionA(self):
print "option A"
def optionB(self):
print "option B"
class SubMenu(Menu):
def __init__(self):
Menu.__init__(self)
self._options = {'c': self.optionC,
'd': self.optionD}
def optionC(self):
# ...
def optionD(self):
# .
..