title: Most efficient way of comparing the contents of two class instances in python
body: <p>I'm looking for the most efficient way of comparing the contents of two class instances. I have a list containing these class instances, and before appending to the list I want to determine if their property values are the same. This may seem trivial to most, but after perusing these forums I wasn't able specific to what I'm trying to do. Also note that I don't have an programming background.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>class BaseObject(object):
      def __init__(self, name=''):
          self._name = name
  
  
      def __repr__(self):
          return '&lt;{0}: \'{1}\'&gt;'.format(self.__class__.__name__, self.name)
  
      def _compare(self, other, *attributes):
          count = 0
          if isinstance(other, self.__class__):
              if len(attributes):
                  for attrib in attributes:
                      if (attrib in self.__dict__.keys()) and (attrib in other.__dict__.keys()):
                          if self.__dict__[attrib] == other.__dict__[attrib]:
                              count += 1
                  return (count == len(attributes))
              else:
                  for attrib in self.__dict__.keys():
                      if (attrib in self.__dict__.keys()) and (attrib in other.__dict__.keys()):
                          if self.__dict__[attrib] == other.__dict__[attrib]:
                              count += 1
                  return (count == len(self.__dict__.keys()))
      def _copy(self):
          return (copy.deepcopy(self))
  </code></pre>
  
  <p>Before adding to my list, I'd do something like:</p>
  
  <pre><code>found = False
  for instance in myList:
      if instance._compare(newInstance): 
          found = True
          Break
  
  if not found: myList.append(newInstance)
  </code></pre>
  
  <p>However I'm unclear whether this is the most efficient or python-ic way of comparing the contents of instances of the same class.</p>