Python创建一系列文本菜单类/对象或装饰函数?

2024-09-29 23:29:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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):
      # .

。。在


Tags: theselfforinitdef菜单classoptions

热门问题