Creating Set of objects of user defined class in 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()

Problem : entry.add() method adds new object every time it is called and increments the items in the table.

This should not happen because

  1. In the add method I am checking that is that object in the table or not , then I am adding that particular object.
  2. Table is a set which is unordered list, which should not have duplicate objects.

Help: is there any way in this set up I can add the object only when it's not in the table.


Solution 1:

You need to implement __eq__ and __hash__ methods to teach Python about how to recognise unique GlobeLearningTable instances.

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

Now your object is comparable, and equal objects will also return equal values for __hash__. This lets set and dict objects store your objects efficiently and detect if it is already present:

>>> demo = set([GlobeLearningTable('a', 10, 'b')])
>>> GlobeLearningTable('a', 10, 'b') in demo
True