所以我正在制作一个菜单结构。它如下:有一个菜单类,它有一个标签,一个元组列表作为它的项目和一个'前一个菜单'。
所以我的想法是,在Main中,之前将是None。菜单中的项目是功能或其他菜单。 (一个函数也可以生成一个菜单)然后当你选择选项1我想将option_1_menu中的'previous'参数设置为main_menu
课程菜单:
def __init__(self, label, options):
self.label = label
self.options = options
self.previous = None
def set_previous(self,previous_menu):
self.previous = previous_menu
def evaluate(self, choice):
if isinstance(self.options[int(choice) - 1][1],Menu):
print('this is a menu')
# Set the previous menu to this menu
self.options[int(choice) - 1][1].set_previous(self)
# Return the new menu
return self.options[int(choice) - 1][1]
# t_current_menu(self.options[int(choice) - 1][1])
else:
print('This is a function')
self.options[int(choice) - 1][1]()
所以我的问题是我将上一个菜单设置到此菜单的行。我基本上把'前一个'设置为我现在的那个。我能以这种方式引用自我来实现这个目标吗?
注意:'这是一个功能'部分仍然是非常多的WiP
答案 0 :(得分:1)
当然,self
是对Menu
实例的完全有效的引用"所以调用someothermenu.set_previous(self)
没问题。添加单元测试@Nsh建议也绝不是一个坏主意(对于此或任何其他功能: - )。