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

Kav*_*hah 7 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. 表是一个无序列表的集合,它不应该有重复的对象.

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

Mar*_*ers 27

您需要实现__eq____hash__方法来教授Python如何识别唯一GlobeLearningTable实例.

class GlobeLearningTable(object):
    def __init__(self,mac,port,dpid):
        self.mac = mac
        self.port = port
        self.dpid = dpid

    def __hash__(self):
        return hash((self.mac, self.port, self.dpid))

    def __eq__(self, other):
        if not isinstance(other, type(self)): return NotImplemented
        return self.mac == other.mac and self.port == other.port and self.dpid == other.dpid
Run Code Online (Sandbox Code Playgroud)

现在您的对象具有可比性,并且相等的对象也将返回相等的值__hash__.这让setdict对象有效地存储你的对象,并检测它是否已经存在:

>>> demo = set([GlobeLearningTable('a', 10, 'b')])
>>> GlobeLearningTable('a', 10, 'b') in demo
True
Run Code Online (Sandbox Code Playgroud)

  • @JohnStrood:为什么它们不相等?如果两个对象上的相同属性设置为“None”,则它们相等并且它们的哈希值相同。如果您正在考虑不同的属性(一个是“mac”,另一个是“port”),它们是不相等的,并且它们的哈希值也不会相同。计算哈希时,元组中的位置*重要*。 (2认同)
  • @JohnStrood:那么你将不得不以不同的方式处理那个用例*,但这不是处理平等的标准方法.请注意,两个不相等的对象具有相同的散列是很好的,如果两个等于具有不同散列的对象,则只会出现问题.如果你想要那些'None`属性,那么只返回`False`,并保持哈希实现不变. (2认同)