title: Python Iterators on Linked List Elements
body: <p>I apologize in advance if this question is misplaced or duplicate.</p>
  
  <p>This question is similar in nature to <a href="https://stackoverflow.com/questions/15710895/doubly-linked-list-iterator-python">doubly Linked list iterator python</a>.</p>
  
  <p>However, unlike the referenced question, I don't wish to create a overarching linked list object which contains a lot of metadata and provides the iterator (they are not necessary for my application). </p>
  
  <p>My question is: Is there any fundamental reason why I should not or can not provide an iterator that doesn't iterate over elements it contains, but instead jumps through distinct element objects that are linked to each other via references?</p>
  
  <p>The iterator is not necessary for proper functioning of the code, but I rather like the syntactic sugar of the <code>for item in</code> construction.</p>
  
  <p>My implementation looks a bit like this (simplified version):</p>
  
  <pre><code>class LinkedAccount:
      def __init__(self, someParameter, nextAccount = None, prevAccount = None):
  
          self.someParameter = someParameter
  
          self.next = nextAccount
          self.prev = prevAccount
          if nextAccount is not None:
              self._tell_next()
          if prevAccount is not None:
              self._tell_prev()
  
      def _tell_next(self):
          if self.next is not None:
              self.next._recv_next(self)
  
      def _recv_next(self,prevAccount):
          self.prev = prevAccount
  
      def _tell_prev(self):
          if self.prev is not None:
              self.prev._recv_prev(self)
  
      def _recv_prev(self,nextAccount):
          self.next = nextAccount
  
  
      def __iter__(self):
          return AccountIterator(self)
  
  class AccountIterator:
      def __init__(self,Account):
          self.Account = Account
  
      def __iter__(self):
          return self
  
      def next(self):
          if self.Account is None:
              raise StopIteration
          else:
              curAccount = self.Account
              self.Account = self.Account.next
              return curAccount
  </code></pre>
  
  <p>The LinkedAccount object provides an iterator that iterates from one LinkedAccount to the next, using the .next parameter already stored in the LinkedAccount object.</p>
  
  <p>This approach appears to work, but the python iterator documentation seems to assume that the iterator is going to walk through elements contained by the parent object. Are there any pitfalls that preclude me doing something like this?</p>
  
  <p>Thanks!</p>