title: Instantiating a class gives dubious results the 2nd time around while looping
body: <p><strong>Edit:</strong> 
  Firstly, thank you @martineau and @jonrsharpe for your prompt reply.
  I was initially hesitant to write a verbose description, but I now realize that I am sacrificing clarity for brevity. (thanks @jonrsharpe for the link).</p>
  
  <p>So here's my attempt to describe what I am upto as succinctly as possible:</p>
  
  <p>I have implemented the Lempel-Ziv-Welch text file compression algorithm in form of a python package. Here's the link to the <a href="https://github.com/pytholic97/LZW-Text-File-Compression" rel="nofollow noreferrer">repository</a>.</p>
  
  <p>Basically, I have a <em>compress</em> class in the <em>lzw.Compress</em> module, which takes in as input the file name(and a bunch of other jargon parameters) and generates the compressed file which is then decompressed by the <em>decompress</em> class within the <em>lzw.Decompress</em> module generating the original file. </p>
  
  <p>Now what I want to do is to compress and decompress a bunch of files of various sizes stored in a directory and save and visualize graphically the  time taken for compression/decompression along with the compression ratio and other metrics. For this, I am iterating over the list of the file names and passing them as parameters to instantiate the compress class and begin compression by calling the encode() method on it as follows:</p>
  
  <pre><code>import os
  
  os.chdir('/path/to/files/to/be/compressed/')
  
  results = dict()
  results['compress_time'] = []
  results['other_metrics'] = []
  file_path = '/path/to/files/to/be/compressed/'
  comp_path = '/path/to/store/compressed/files/'
  decomp_path = '/path/to/store/decompressed/file'
  files = [_ for _ in os.listdir()]
  for f in files:
      from lzw.Compress import compress as comp
      from lzw.Decompress import decompress as decomp
  
      c = comp(file_path+f,comp_path) #passing the input file and the output path for storing compressed file.
      c.encode()               
      #Then measure time required for comression using time.monotonic()
  
      del c
      del comp
  
      d = decomp('/path/to/compressed/file',decomp_path) #Decompressing
      d.decode()
      #Then measure time required for decompression using 
      #time.monotonic()
      #append metrics to lists in the results dict for this particular 
      #file
  
      if decompressed_file_size != original_file_size:
          print("error")
          break
      del d 
      del decomp
  </code></pre>
  
  <p>I have run this code independently for each file without the for loop and have achieved compression and decompression successfully. So there are no problems in the files I wish to compress.</p>
  
  <p>What happens is that whenever I run this loop, the first file (the first iteration) runs successfully and the on the next iteration, after the entire process happens for the 2nd file, "error" is printed and the loop exits. I have tried reordering the list or even reversing it(maybe a particular file is having a problem), but to no avail.</p>
  
  <p>For the second file/iteration, the decompressed file contents are dubious(not matching the original file). Typically, the decompressed file size is nearly double that of the original.
  I strongly suspect that there is something to do with the variables of the class/package retaining their state somehow among different iterations of the loop. (To counter this I am deleting both the instance and the class at the end of the loop as shown in the above snippet, but no success.)
  I have also tried to import the classes outside the loop, but no success.  </p>
  
  <p><strong>P.S.:</strong> I am a python newbie and don't have much of an expertise, so forgive me for not being "pythonic" in my exposition and raising a rather naive issue.</p>
  
  <p><strong>Update:</strong>
  Thanks to @martineau, one of the problem was regarding the importing of global variables from another submodule. 
  But there was another issue which crept in owing to my superficial  knowledge about the 'del' operator in python3.
  I have this <a href="https://en.wikipedia.org/wiki/Trie" rel="nofollow noreferrer">trie</a> data structure in my program which is basically just similar to a binary tree. 
  I had a self_destruct method to delete the tree as follows:</p>
  
  <pre><code>class trie():
  
  def __init__(self):
      self.next = {}
      self.value = None
      self.addr = None
  
  def insert(self, word=str(),addr=int()):
      node = self
  
      for index,letter in enumerate(word):
          if letter in node.next.keys():
              node = node.next[letter]
          else:
              node.next[letter] = trie()
              node = node.next[letter]
          if index == len(word) - 1:
              node.value = word
              node.addr = addr
  def self_destruct(self):
      node = self
  
      if node.next == {}:
          return
  
      for i in node.next.keys():
          node.next[i].self_destruct()
  
      del node
  </code></pre>
  
  <p>Turns out that this C-like recursive deletion of objects makes no sense in python as here simply its association in the namespace is removed while the real work is done by the garbage collector.
  Still, its kinda weird why python is retaining the state/association of variables even on creating a new object(as shown in my loop snippet in the edit).
  So 2 things solved the problem. Firstly, I removed the global variables and made them local to the module where I need them(so no need to import). Also, I deleted the self_destruct method of the trie and simple did: <strong>del root</strong> where root = trie() after use.</p>
  
  <p>Thanks @martineau &amp; @jonrsharpe.</p>