title: Custom class with pop() method not returning correct new length of list
body: <p>I have a custom class in Python that's supposed to behave like a Stack. For some reason when I'm trying to traverse a tree and add nodes to it, everytime i pop from the list it doesn't seem to change its size.</p>
  <p>The code is breaking infinitely looping at the print statements, but I'm not sure why. Pop() should be reducing the number of items, but len(self.items) either returns 3 or 4 every loop.</p>
  <p><strong>Class definitions:</strong></p>
  <pre><code>import collections
  
  
  
  class Stack:
      def __init__(self, items=[]):
          self.items = items
          self.idx = 0
  
      def __iter__(self):
          return self
      def __next__(self):
          self.idx += 1
          try:
              return self.items[0-self.idx]
          except IndexError:
              self.idx = 0
              raise StopIteration
  
  
      # isEmpty() - returns True if stack is empty and false otherwise
      def isEmpty(self):
          if self.items:
              return False
          return True
      # push() - Insert item at the top of the stack
      def push(self,item):
          self.items.append(item)
          return
  
      # pop() - Remove and return element from top of the stack,
      # provided it is not empty
      def pop(self):
          return self.items.pop()
  
      # also known as peek() - return top element from stack without removing,
      #
      def top(self):
          return self.items[-1]
  
  class Node():
      def __init__(self,value=None):
          self.value = value
          self.left = None
          self.right = None
  
  class Tree:
      def __init__(self, array):
          self.root = None
          self.items = []
          self.size = 0
  
          if array is not None:
              # check if input array is an actual sequence or scalar
              '''
              Items here are unchanged, only converts to Node in insert() function
              '''
              if isinstance(array,collections.abc.Sequence):
                  for item in array:
                      self.insert(item)
                      self.size = self.size +1
              # if input is a single item
              else:
                  self.insert(item)
                  self.size = self.size +1
  
      # insert() - add element to corresponding location, then shuffle
      #
      def root(self):
          return self.root
  
      # returns parent of node v,
      # error occurs if v is root
      def parent(v):
          if v == self.items[0]:
              raise ValueError(&quot;ADT Tree - Parent can't be root (use root() instead)&quot;, e)
          return
  
      # returns an iterator to enumerate the children of node v
      def children(self,v):
          return
      # test wheter node v is internal
      def isInternal(self,v):
          if v.left is not None or v.right is not None:
              return True
          return False
      #test whether node v is external (i.e. leaf)
      def isExternal(self,v):
          return not self.isInternal(v)
      # test wheter node v is root
      def isRoot(v):
          return v == self.items[0]
  
  
  # inherit Tree parent class
  # ADT.BinaryTree(array)
  # insert(), inOrder(node), preOrder(node), postOrder(node)
  class BinaryTree(Tree):
      # inherit Tree parent class
      def __init__(self, array=tuple()):
          # inherit Tree methods EXCEPT init populating items
          super().__init__(None)
  
          # list for outputting pre/post/in order  traversals
          self.list = list()
          self.count = 0
          for item in array:
              self.insert(item)
  
  
      # test wheter node v is internal
      def isInternal(self,v):
          if v.left or v.right:
              return True
          else:
              return False
          return
      #test whether node v is external (i.e. leaf)
      def isExternal(self,v):
          if v.left or v.right:
              return False
          else:
              return True
          return
  
  
  
  
      # public
      def insert(self,value):
          self.root = self.insertRecur(self.root, value)
          self.count += 1
  
      # private recursive
      def insertRecur(self,current, value):
          # base case
          if (current is None):
              return Node(value)
          # traverse left
          if (value &lt; current.value):
              current.left = self.insertRecur(current.left,value)
  
          # traverse right
          elif (value &gt; current.value):
              current.right = self.insertRecur(current.right,value)
  
          # if current has same value as node, ignore value as a duplicate
          return current
  
  
      # accepts a node, returns a list of elements in subtree rooted at node In-order
      # L node R
      def inOrder(self,node):
          self.list = list()
          self._inOrder(node)
          return self.list
  
      # accepts a node, returns a list of elements in subtree rooted at node Pre-order
      # node L R
      def preOrder(self,node):
          self.list = list()
          self._preOrder(node)
          return self.list
      # accepts a node, returns a list of elements in subtree rooted at node Post-order
      # L R node
      def postOrder(self,node):
          self.list = list()
          self._postOrder(node)
          return self.list
  
  
      # L root R
      # prints current node after traversing left children AND before traversing right
      def _inOrder(self,node):
          if node is not None:
              self._inOrder(node.left)
              self.list.append(node)
              #print(&quot;{}&quot;.format(node.value),end=&quot; &quot;)
              self._inOrder(node.right)
          return
  
      # root L R
      def _preOrder(self,node):
          if node is not None:
              self.list.append(node)
              #print(&quot;{}&quot;.format(node.value),end=&quot; &quot;)
              self._preOrder(node.left)
              self._preOrder(node.right)
          return
  
      # R root L
      def _postOrder(self,node):
          if node is not None:
              self._postOrder(node.right)
              self.list.append(node)
              #print(&quot;{}&quot;.format(node.value),end=&quot; &quot;)
              self._postOrder(node.left)
          return
  </code></pre>
  <p><strong>Main code</strong>
  (issue is around the print statements)</p>
  <pre><code>def main():
      test = (4,5,8,9,2,1,10,3,6,7)
      newtree = BinaryTree(test)
  
      stk_c = Stack()
      stk_f = Stack()
      node = newtree.root
      prev = None
  
  
  
      while node or not stk_c.isEmpty():
          # if node has already been travelled,
          # traverse its left child
          if node is not None:
              stk_c.push(node)
              node = node.left
  
          elif node is None:
              node = stk_c.top()
              if node.right is None or node.right == prev:
                  print(&quot;before popping stk_c&quot; ,len(stk_c.items))
                  stk_f.push(node.value)
                  stk_c.pop()
                  print(&quot;after popping stk_c&quot;, len(stk_c.items))
                  prev=node
                  node = None
              else:
                  node = node.right
  
  
  if __name__ == &quot;__main__&quot;:
      main()
  </code></pre>