title: What is a good way in object oriented programing to create lists and filter various results?
body: <p>I have a method which creates a list of files of a directory, but i have multiple other methods which do the same plus some additional filtering during the loop.</p>
  
  <p>This looks like a very bad solution to me, i'm very interested in how to do this more elegant.</p>
  
  <p>Here is a simple example of what i'm doing:</p>
  
  <pre><code>class foo(object):
  
      def read_files_in_dir(...)
          ....
  
      def filter_x(...):
          for file in read_files_in_dir(...):
              if file.startswith("x"):
                  yield file
  
      def filter_y(....):
          for file in read_files_in_dir(...):
              if file.startswith("y"):
                  yield file
  
      ...
  </code></pre>
  
  <p>I don't want to merge all the results of the <code>filter</code>-functions, every result has a different meaning since i'm reading different directories which require different ways of filtering.</p>
  
  <p>Is there any way to have a central function for the loop and the possibility of different filtering options without parametrizing the hell out of it?</p>
  
  <p>// EDIT</p>
  
  <p>I'm doing it the following way now:</p>
  
  <pre><code>class Something(object):
  
      @staticmethod
      def read_files_in_dir(self, directory, filter_func=lambda _: return True):
          for file in directory:
              if filter_func(file):
                  yield file
  
      class Filters(object):
  
          @staticmethod
          def filter1(file):
              return True if *some_condition* else False
  </code></pre>
  
  <p>I run something like this then:</p>
  
  <pre><code>filtered = Something.read_files_in_dir(*directory*, Something.Filters.filter1)
  </code></pre>
  
  <p>Any suggestions for improvement?</p>