title: What is the correct (or best) way to subclass the Python set class, adding a new instance variable?
body: <p>I'm implementing an object that is almost identical to a set, but requires an extra instance variable, so I am subclassing the built-in set object. What is the best way to make sure that the value of this variable is copied when one of my objects is copied?</p>
  
  <p>Using the old sets module, the following code worked perfectly:</p>
  
  <pre><code>import sets
  class Fooset(sets.Set):
      def __init__(self, s = []):
          sets.Set.__init__(self, s)
          if isinstance(s, Fooset):
              self.foo = s.foo
          else:
              self.foo = 'default'
  f = Fooset([1,2,4])
  f.foo = 'bar'
  assert( (f | f).foo == 'bar')
  </code></pre>
  
  <p>but this does not work using the built-in set module.</p>
  
  <p>The only solution that I can see is to override every single method that returns a copied set object... in which case I might as well not bother subclassing the set object. Surely there is a standard way to do this?</p>
  
  <p>(To clarify, the following code does <em>not</em> work (the assertion fails):</p>
  
  <pre><code>class Fooset(set):
      def __init__(self, s = []):
          set.__init__(self, s)
          if isinstance(s, Fooset):
              self.foo = s.foo
          else:
              self.foo = 'default'
  
  f = Fooset([1,2,4])
  f.foo = 'bar'
  assert( (f | f).foo == 'bar')
  </code></pre>
  
  <p>)</p>