我有2个班级:
1包含数据,另一个包含数组
如何创建添加该类的函数(如列表)
class Event(object):
def __init__(self):
self.msg_idx=0 ## the message from the panel
self.evt_id=0 ## the event id given by the server
self.Panel_Number=0 ## the panel number
self.line=""
def updateData(self,msg,event,panel,line):
self.msg_idx =msg
self.evt_id = event
self.Panel_Number = panel
self.line =line
class Events(object):
def __init__(self):
self.event =[]
def addEvent(self, Server_Event):
????????
答案 0 :(得分:1)
字面意思只是追加:
class Event(object):
def __init__(self):
self.msg_idx=0 ## the message from the panel
self.evt_id=0 ## the event id given by the server
self.Panel_Number=0 ## the panel number
self.line=""
def updateData(self,msg,event,panel,line):
self.msg_idx =msg
self.evt_id = event
self.Panel_Number = panel
self.line =line
class Events(object):
def __init__(self):
self.event =[]
def addEvent(self, Server_Event):
self.event.append(Server_Event)
something = Event()
somethings = Events()
somethings.addEvent(something)
我会问你为什么现在需要这样一个类,当你可以使用一个名为events的列表时。但我以前做过类似的事情,我知道它可以更容易地在顶部添加元数据。
但是如果您只是在没有任何额外分析的情况下使用它,那么使用列表会更有效。