title: custom iterator class vs. generator?
body: <p>The following is an illustration. I'd like to understand in general whether there is a reason for defining a custom iterator class when a generator function can be defined instead.</p>
  
  <p>I need to iterate over a sequence converting every element to int, e.g.</p>
  
  <pre><code># seq is a sequence of strings or in general anything convertible to int
  def f(seq):
      # ...
      g(int_iter(seq))
  
  # iseq is a numeric sequence
  def g(iseq):
      it = iter(iseq)
      # ...
  </code></pre>
  
  <p>I can use either a custom iterator class:</p>
  
  <pre><code># iterator converting elements it iterates over to int
  class int_iter:
      def __init__(self, iterable):
          self.it = iter(iterable)
  
      def __iter__(self):
          return self
  
      def __next__(self):
          return int(next(self.it))
  </code></pre>
  
  <p>or a generator function:</p>
  
  <pre><code>def int_iter(seq):
      return (int(i) for i in seq)
  </code></pre>
  
  <p>Are these solutions always interchangeable?<br>
  Are they equivalently efficient (time- and space-wise)?<br>
  Stylistically, is any of them considered better?  </p>
  
  <p>Thanks! </p>