Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7856391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:41:32+00:00 2026-06-02T20:41:32+00:00

I frequently use a generator that returns a certain class. What I’d like to

  • 0

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.

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 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 types.GeneratorType.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:41:33+00:00Added an answer on June 2, 2026 at 8:41 pm

    A generator is something that behaves like an iterator, but the sequence it represents is, unlike a tuple or a list, generated lazily for each iteration step. The common ways to create generators are by using generator expressions or with the yield statement; any other mechanism, if it exists, is black magic and you should stay away from it.

    Therefore, you should forget about types.GeneratorType and inheriting from it. You normally wrap or chain generators together. You can do that with generator expressions, as you have in your example code or you can use the wonderful itertools standard module.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to use Ruby methods to generate the markup that appears frequently in
Hi I have SVN repo that I frequently use for change management. However after
I have a 'foreach' macro I use frequently in C++ that works for most
I have a function that I use quite frequently, which allows me to write
I have an application that makes use of frequently updated lists. So for example,
I frequently use the STL containers but have never used the STL algorithms that
I frequently use fork in programs that also have END { ... } blocks:
I frequently use git stash and git stash pop to save and restore changes
I frequently use the following pattern to set an upper bound to the running
I use Visual C++ 2008 in Visual Studio 2008. I frequently use the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.