0
table = set([])
class GlobeLearningTable(object):
def __init__(self,mac,port,dpid):
self.mac = mac
self.port = port
self.dpid = dpid
def add(self):
global table
if self not in table:
table.add(self)
class LearningSwitch(object):
def __init__ (self, connection, transparent):
self.connection = connection
self.transparent = transparent
self.macToPort = {}
connection.addListeners(self)
self.hold_down_expired = _flood_delay == 0
def _handle_PacketIn (self, event):
packet = event.parsed
self.macToPort[packet.src] = event.port # 1
packet_src = str(packet.src)
packet_mac = packet_src.upper()
entry = GlobeLearningTable(packet_mac, event.port, dpid_to_str(self.connection.dpid))
entry.add()
問題:entry.add()
方法每次調用時都會添加新對象,並增加表中的項。在Python中創建用戶定義類的對象集
這是不應該發生,因爲
- 在添加方法,我檢查是在表對象或沒有,然後我補充說,特定的對象。
- 表是一個無序列表,它不應該有重複的對象。
幫助:有沒有什麼辦法可以在這個設置中添加對象,只有當它不在表格中。
你可能是指'表=集()',而不是'集合([])' – ersran9