title: Forcing compatibility of inherited types (Python)
body: <p>Assume someone gave you an existing pair of classes that work together:</p>
  
  <pre><code>class non_unique_collection:
      def __init__(self):
          self.all_items = []
      def add_new_item(self, item):
          self.all_items.append(item)
      def __len__(self):
          return len(self.all_items)
      ## ...
      ## there are several other useful functions here
      ## ...
      def overlap(self, n_u_tuple):
          # do some things here
          pass
  
  class non_unique_ranked_collection:
      def __init__(self, some_scores_and_items):
          self.ranked_items = sorted(some_scores_and_items)
  </code></pre>
  
  <p>I want to use many functions of that class (e.g. <code>__len__</code>), but I want to change something very basic:</p>
  
  <pre><code>class unique_collection(non_unique_collection):
      def __init__(self):
          self.all_items = set([])
      def add_new_item(self, item):
          self.all_items.add(item)
  </code></pre>
  
  <p>I also want to use my derived <code>unique_collection</code> class with another new class <code>unique_ranked_collection</code>:</p>
  
  <pre><code>class unique_ranked_collection(non_unique_ranked_collection):
      def __init__(self, some_scores_and_items):
          # average scores for duplicate items
          items_to_scores = {}
          for score, item in some_scores_and_items:
              if item in items_to_scores:
                  items_to_scores[item].append(score)
              else:
                  items_to_scores[item] = [ score ]
          average_scores_and_unique_items = dict( [ (sum(items_to_scores[item])/float(len(items_to_scores[item])), item) for item in items_to_scores ] )
          self.ranked_items = sorted(average_scores_and_unique_items)
  </code></pre>
  
  <p>Here is my question:</p>
  
  <p>What is the <em>best</em> way to make it so that the <code>overlap</code> function can be used with a <code>non_unique_collection</code> and a <code>non_unique_ranked_collection</code> or with a <code>unique_collection</code> and a <code>unique_ranked_collection</code> (but <em>not</em> between a <code>unique...</code> and <code>non_unique...</code> types)? I started to put in some assert statements using <code>isinstance</code>, but it started to look messy, and I wanted to ask for advice.</p>
  
  <p>The best thing I could think of was to have two base classes that both the <code>unique...</code> and <code>non_unique...</code> types inherit from. What do you think about that?</p>
  
  <p>Thanks a lot.</p>
  
  <p>PS-- I know this specific problem that the code is written for is pretty uninteresting, but it's just an illustration.</p>