我正在创建两个类,一个需要保存另一个的实例。但是我不知道如何正确地初始化这个。你知道吗
class Buttons:
def __init__(self, number, scene):
self.DICT = {}
self.number = number
self.DICT[number] = scene
def add_btn(self, number, scene):
self.DICT[number] = scene
class Switches:
enclosure_name = ""
gatewate_name = ""
enclosure_id = 0
switch_name = ""
switch_location = ""
switch_device_id = 0
switch_mac = 0
switch_termination = 0
switch_group = 0
Buttons buttons = Buttons()
我计划创建许多开关,每个开关有2到6个按钮。每个按钮都有一个数字和一个动作。如何将按钮变量放入开关中?你知道吗
我的理解是,每个
Switch
将包含一个Buttons
对象,其中包含一个表示多个按钮的字典。你知道吗Switches
类是多个开关的表示:只需给每个开关一个buttons属性:
在
Buttons
对象中创建开关过程时:您仍然可以访问交换机的底层按钮。例如,向第10个开关添加按钮:
如果你想成为一个花哨的人,你可以实现一个
__getitem__
,这样你就可以直接索引switch
。内部Switches
:使用此方法可以直接添加按钮:
相关问题 更多 >
编程相关推荐