相关疑难解决方法(0)

在python中创建用户定义类的对象集

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()
Run Code Online (Sandbox Code Playgroud)

问题:entry.add()方法每次调用时都会添加新对象,并递增表中的项目.

这不应该发生,因为

  1. 在add方法中,我正在检查表中是否有该对象,然后我添加了该特定对象.
  2. 表是一个无序列表的集合,它不应该有重复的对象.

帮助:在这个设置中有什么办法我只能在不在表格中时添加对象.

python

7
推荐指数
1
解决办法
7789
查看次数

标签 统计

python ×1