I often use a generator that returns a specific class. What I would like to do is subclass the generator class so that I can use the methods on it that are suitable for the generators that give instances of this class. For example, one of the things I would like to do is a method that returns a generator that filters the base generator.
I want to do something like this:
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)
The class of clothes that I want to consider as a collection of clothes. The reason I don’t subclass the collection is because I rarely want to use the entire clothing collection as it is, and usually just need to filter it further. However, I often need various filtered clothing collections. If possible, I would like the clothes to be a generator, as I intend to use them, but I get an error when trying to subclass types.GeneratorType.
source
share