title: OOP way to implement class for comparison in Python
body: <p>Using Python, I am trying to implement a set of types including a "don't care" type, for fuzzy matching.  I have implemented it like so:</p>
  
  <pre><code>class Matchable(object):        
      def __init__(self, match_type = 'DEFAULT'):
          self.match_type = match_type
  
      def __eq__(self, other):
          return (self.match_type == 'DONTCARE' or other.match_type == 'DONTCARE' \
  or self.match_type == other.match_type)
  </code></pre>
  
  <p>Coming from an OO background, this solution seems inelegant; using the Matchable class results in ugly code.  I'd prefer to eliminate match_type, and instead make each type its own class inherited from a superclass, then use type checking to do the comparisons.  However type checking appears to be generally frowned upon:</p>
  
  <p><a href="http://www.canonical.org/~kragen/isinstance/" rel="nofollow">http://www.canonical.org/~kragen/isinstance/</a></p>
  
  <p>Is there are better (more pythonic) way to implement this functionality?</p>
  
  <p><strong>Note:</strong> I'm aware of the large number of questions and answers about Python "enums", and it may be that one of those answers is appropriate.  The requirement for the overridden __ eq __ function complicates matters, and I haven't seen a way to use the proposed enum implementations for this case.</p>
  
  <p>The best OO way I can come up with of doing this is:</p>
  
  <pre><code>class Match(object):
      def __eq__(self, other):
          return isinstance(self, DontCare) or isinstance(other, DontCare) or type(self) == type(other)
  
  class DontCare(Match):
      pass
  
  class A(Match):
      pass
  
  class B(Match):
      pass
  
  d = DontCare()
  a = A()
  b = B()
  
  
  print d == a
  True
  print a == d
  True
  print d == b
  True
  print a == b
  False
  print d == 1
  True
  print a == 1
  False
  </code></pre>