title: Is it possible and advisable to subclass a generator function?
body: <p>I frequently use a generator that returns a certain class. What I'd like to do is subclass the generator class so that I can use methods on it that are appropriate for generators that yield instances of that class. For example, one of the things I'd like to do is have a method that returns a generator that filters the base generator.</p>
  
  <p>I want to do something like this:</p>
  
  <pre><code>class Clothes(object):
      def __init__(self, generator):
          self.generator = generator
  
      def get_red(self):
          return (c for c in self.generator if c.color=="red")
  
      def get_hats(self):
          return (c for c in self.generator if c.headgear)
  </code></pre>
  
  <p>The clothes class I want to treat as a collection of clothes. The reason I'm not subclassing a collection is that I rarely want to use the whole collection of clothes as is, and usually just need to filter it further. However, I often need the various filtered collections of clothes. If possible, I'd like Clothes to be a generator itself, as that's how I intend to use it, but I get an error when trying to subclass <code>types.GeneratorType</code>.</p>