title: Returning object before modifying it in __next__()
body: <p>I'm programming an iterator object that returns modifications of itself and I want the original object to be part of the iteration process, so it needs to be returned before being modified.</p>
  <p>...but:</p>
  <ul>
  <li>Creating a deepcopy of the object to return is very time-consuming.</li>
  <li>The object have lots of attributes, so defining auxiliary variables doesn't seem an elegant solution.</li>
  <li><code>yield</code> inside <code>__next__</code> will not work because it returns a different type (generator) in each iteration instead.</li>
  <li>I could use <code>yield</code> inside <code>__iter__</code> to get an iterator but then I couldn't use <code>__next__</code> to get on-demand iterations (my object is a Turing machine, so a controlled step-by-step print of the tape would be nice).</li>
  </ul>
  <p>An example of working, but inefficient, code:</p>
  <pre><code>from copy import deepcopy
  
  class MyObject():
      def __init__(self, n):
          self.n = n
     
      def __str__(self):
          return str(self.n)
  
      def __iter__(self):
          return self
  
      def __next__(self):
          if self.n &gt;= 10:
              raise StopIteration
  
          self_copy = deepcopy(self)
          self.n += 1
          return self_copy
  
  for x in MyObject(n=7):
      print(x)
  </code></pre>
  <p>Output:</p>
  <pre><code>7
  8
  9
  </code></pre>
  <p>I'm probably missing something here but I can't figure it out.</p>